Skip to content

Commit

Permalink
Create win64-gcc dist builder variant. Fix remaining compiler issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
FeepingCreature committed Feb 23, 2024
1 parent 96e7095 commit 68d1975
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 47 deletions.
78 changes: 66 additions & 12 deletions release-gcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,30 @@ then
fi
VERSION="$1"

LLVM=0
NTFLAGS="-O"
BUILD=""
STAGE1FLAGS="-O"
STAGE2FLAGS="-O"
RELEASE=""
ARCHS=64
export WINEDEBUG=-all

if [ $(basename $0) == "release-llvm.sh" ]
then
LLVM=1
NTFLAGS="${NTFLAGS} -version=LLVMBackend"
BUILD="llvm"
STAGE1FLAGS="${STAGE1FLAGS} -version=LLVMBackend"
STAGE2FLAGS="${STAGE2FLAGS} -macro-version=LLVMBackend -version=LLVMBackend"
RELEASE="neat-$VERSION-llvm"
elif [ $(basename $0) == "release-gcc.sh" ]
then
BUILD="gcc"
ARCHS="32 64"
RELEASE="neat-$VERSION-gcc"
elif [ $(basename $0) == "release-win64-gcc.sh" ]
then
BUILD="win64-gcc"
RELEASE="neat-$VERSION-win64-gcc"
STAGE1FLAGS="${STAGE1FLAGS} -target=windows -dllsafe"
STAGE2FLAGS="${STAGE2FLAGS} -macro-target=windows -macro-dllsafe -target=windows -dllsafe"
else
echo "Unknown release name $(basename $0)"
exit 1
Expand Down Expand Up @@ -48,14 +58,14 @@ do
ARCHFLAGS = "-m32"
fi
build/neat -backend=c -Pcompiler:src -dump-intermediates build/intermediates.txt src/main.nt -c \
$ARCHFLAGS $NTFLAGS
$ARCHFLAGS $STAGE1FLAGS

mkdir $TARGET/intermediate_$ARCH
cp $(tail -n +2 build/intermediates.txt) $TARGET/intermediate_$ARCH/
head -1 build/intermediates.txt > $TARGET/main.txt
done

if [ $LLVM -eq 0 ]
if [ "$BUILD" = "gcc" ]
then
cat > $TARGET/build.sh <<EOT
#!/usr/bin/env bash
Expand Down Expand Up @@ -114,10 +124,41 @@ then
-macro-m32
EOI
fi
./neat_bootstrap -j src/main.nt $NTFLAGS -o neat
./neat_bootstrap -j src/main.nt $STAGE2FLAGS -o neat
rm neat_bootstrap
EOT
else
chmod +x $TARGET/build.sh
elif [ "$BUILD" = "win64-gcc" ]
then
cat > $TARGET/build.bat <<EOT
SETLOCAL
FOR /F %%I IN (main.txt) DO SET "DMAIN=-DMAIN=%%I"
SET "CFLAGS=%CFLAGS% -Ofast -fno-strict-aliasing -pthread"
SET "LINKFLAGS=-Wl,/STACK:8388608"
FOR /R %%F IN (*.c) DO (
SET "obj=%%~dpnF.o"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO gcc -m64 -w -c -g -fpic -rdynamic -fno-strict-aliasing %DMAIN% %%~fF -o !obj!
gcc -m64 -w -c -g -fpic -rdynamic -fno-strict-aliasing %DMAIN% %%~fF -o !obj!
CALL SET obj=%%obj:\\=@%%
CALL SET obj=%%obj:@=\\\\%%
ECHO !obj! >> objects.txt
ENDLOCAL
)
gcc -m64 -g -fpic -rdynamic -fno-strict-aliasing @objects.txt -o neat_bootstrap -lm %CFLAGS% %LINKFLAGS%
REM FOR /F "delims=" %%I IN (objects.txt) DO DEL %%I
ECHO -syspackage compiler:src > neat.ini
ECHO -backend=c >> neat.ini
ECHO -target=x86_64-w64-mingw32 >> neat.ini
ECHO -macro-target=x86_64-w64-mingw32 >> neat.ini
ECHO -macro-dllsafe >> neat.ini
ECHO -running-compiler-version=$VERSION >> neat.ini
neat_bootstrap -j src/main.nt $STAGE2FLAGS -o neat
REM DEL neat_bootstrap
ENDLOCAL
EOT
elif [ "$BUILD" = "llvm" ]
then
. ./find-llvm-config.sh
CFLAGS="${CFLAGS:+ }-I$($LLVM_CONFIG --includedir) -L$($LLVM_CONFIG --libdir)"
install -v ./find-llvm-config.sh "$TARGET"
Expand Down Expand Up @@ -167,9 +208,10 @@ done
for i in \$(seq \$JOBS); do wait -n; done
gcc \$ARCHFLAG -fpic -rdynamic -fno-strict-aliasing \${OBJECTS[@]} -o neat_bootstrap -ldl -lm -lLLVM \$CFLAGS
rm \${OBJECTS[@]}
./neat_bootstrap -j -macro-backend=c src/main.nt \${LLVM_NTFLAGS} $NTFLAGS -o neat
./neat_bootstrap -j -macro-backend=c src/main.nt \${LLVM_NTFLAGS} $STAGE2FLAGS -o neat
rm neat_bootstrap
EOT
chmod +x $TARGET/build.sh
cat > $TARGET/neat.ini <<EOT
-syspackage compiler:src
-backend=llvm
Expand All @@ -179,16 +221,28 @@ EOT
-running-compiler-version=$VERSION
-extra-cflags=\$ARCHFLAG
EOT
else
echo "Unknown build '$BUILD'!"
exit 1
fi
chmod +x $TARGET/build.sh

for ARCH in $ARCHS
do
echo "Test $ARCH"
TEST_TARGET="test_${RELEASE}_${ARCH}"
cp -R "${TARGET}" "${TEST_TARGET}"
(cd "${TEST_TARGET}" && ./build.sh)
NEAT="${TEST_TARGET}"/neat ./runtests.sh
if [ "$BUILD" = "win64-gcc" ]; then
(cd "${TEST_TARGET}" && wine build.bat)
else
(cd "${TEST_TARGET}" && ./build.sh)
fi
if [ "$BUILD" = "win64-gcc" ]; then
NEAT="wine \"${TEST_TARGET}\"/neat.exe"
./runtests.sh win64
else
NEAT="${TEST_TARGET}"/neat
./runtests.sh
fi
rm -rf "${TEST_TARGET}"
done
(cd "$RELEASE"
Expand Down
1 change: 1 addition & 0 deletions release-win64-gcc.sh
29 changes: 17 additions & 12 deletions src/backend/c.nt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class CBackendModule : BackendModule {
|| name == "strlen" || name == "realpath" || name == "getenv"
|| name == "atoi" || name == "atof" || name == "atoll" || name == "memcmp"
|| name == "memset" || name == "free" || name == "exit"
|| name == "_fullpath"
) return; // in header, don't redeclare - may conflict
mut string qualifier = "";
if (this.platform.win64) {
Expand Down Expand Up @@ -665,17 +666,7 @@ class CBackendFunction : BackendFunction {
return Value("int", ltoa(value), exactType=true);
}
(:stringLiteral, string text): {
mut string escapedText;
// TODO ord()
for (i in 0 .. text.length) {
if (text[i .. i + 1] == "\"") escapedText ~= "\\\"";
else if (text[i .. i + 1] == "\\") escapedText ~= "\\\\";
else if (text[i .. i + 1] == "\r") escapedText ~= "\\r";
else if (text[i .. i + 1] == "\n") escapedText ~= "\\n";
else if (text[i .. i + 1] == "\0") escapedText ~= "\\0";
else escapedText ~= text[i .. i + 1];
}
return Value("char*", "\"" ~ escapedText ~ "\"", exactType=true);
return Value("char*", "\"" ~ text.escape ~ "\"", exactType=true);
}
(:zeroLiteral, BackendType type): {
if (type.size == 0) return Value.none;
Expand Down Expand Up @@ -893,7 +884,7 @@ class CBackendFunction : BackendFunction {
this.print("}");
return Value.none;
(:setLocation, BackendLocation loc):
this.print("#line $(loc.line+1) \"$(loc.file)\"");
this.print("#line $(loc.line+1) \"$(loc.file.escape)\"");
return Value.none;
}
}
Expand Down Expand Up @@ -1194,3 +1185,17 @@ string repr(Instr instr) {
(:setLocation, BackendLocation loc): "setLocation($(loc.line + 1), \"$(loc.file)\")"
);
}

private string escape(string s) {
mut string result;
// TODO ord()
for (i in 0 .. s.length) {
if (s[i .. i + 1] == "\"") result ~= "\\\"";
else if (s[i .. i + 1] == "\\") result ~= "\\\\";
else if (s[i .. i + 1] == "\r") result ~= "\\r";
else if (s[i .. i + 1] == "\n") result ~= "\\n";
else if (s[i .. i + 1] == "\0") result ~= "\\0";
else result ~= s[i .. i + 1];
}
return result;
}
1 change: 1 addition & 0 deletions src/c/windows.nt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ alias GetFileAttributes = GetFileAttributesA;
alias INVALID_FILE_ATTRIBUTES = -1;

extern(C) DWORD GetModuleFileNameA(void* hModule, char* lpFilename, DWORD nSize);
extern(C) char* _fullpath(char* absPath, char* relPath, size_t maxLength);

alias GetModuleFileName = GetModuleFileNameA;
alias MAX_PATH = 260;
16 changes: 8 additions & 8 deletions src/helpers.nt
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,14 @@ string join(string[] array, string sep) {

bool exists(string file) {
auto fn = toStringz(file);
int F_OK = 0;
bool ret = access(fn, F_OK) != -1;
version (windows) {
import c.windows : GetFileAttributes, INVALID_FILE_ATTRIBUTES;
auto attributes = GetFileAttributes(fn);
bool ret = attributes != INVALID_FILE_ATTRIBUTES;
} else {
int F_OK = 0;
bool ret = access(fn, F_OK) != -1;
}
free(fn);
return ret;
}
Expand Down Expand Up @@ -122,9 +128,3 @@ bool startsWith(string haystack, string needle) {
if (haystack.length < needle.length) return false;
return haystack[0 .. needle.length] == needle;
}

version (windows) {
string realpath(string path) {
return path;
}
}
16 changes: 13 additions & 3 deletions src/main.nt
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,12 @@ ASTPackage parsePackage(mut string arg, bool syspkg)
}

string resolvePath(ASTPackage pak, string execPath) {
if (!pak.syspkg) return realpath(pak.path);
return realpath("$execPath/$(pak.path)");
if (!pak.syspkg) return pak.path.realpath;
version (firstpass) {
return (execPath ~ "/" ~ pak.path).realpath;
} else {
return execPath.sub(pak.path).realpath;
}
}

Package[] resolvePackages(ASTPackage[] astPackages, string execPath) {
Expand Down Expand Up @@ -956,6 +960,8 @@ class Options
auto lines = path.read.split("\n");
mut string[] args;
for (line in lines) {
mut auto line = line;
if (line.endsWith("\r")) line = line[0 .. $ - 1];
auto words = line.split(" ");
args ~= words[0];
mut int used = 1;
Expand Down Expand Up @@ -1140,7 +1146,11 @@ long long int compiler_hash_mult() { return $(mult)LL; }
else
flags ~= "-DMAIN=$(mainName?)";
// flags ~= fileArgs;
auto fileArgsFile = "build/objects.$(tmpId).txt";
version (firstpass) {
auto fileArgsFile = ".obj/objects.$(tmpId).txt";
} else {
auto fileArgsFile = ".obj".sub("objects.$(tmpId).txt");
}
fileArgsFile.write(fileArgs.map(a => "$a\n").array.join(""));
flags ~= "@$fileArgsFile";
version (firstpass) {
Expand Down
22 changes: 14 additions & 8 deletions src/neat/stuff.nt
Original file line number Diff line number Diff line change
Expand Up @@ -4291,29 +4291,35 @@ string moduleToFile(string module_)

(string, Package | Error) findFileInPackage(string filename, Package pak, LocRange locRange)
{
string fileInPackage = pak.path ~ "/" ~ filename;
version (firstpass) {
alias sub = (a, b) => a ~ "/" ~ b;
} else {
import package(compiler).std.file : sub;
}

string fileInPackage = pak.path.sub(filename);
// print("$fileInPackage: $(exists(fileInPackage))");
if (exists(fileInPackage)) {
// print(filename ~ " in package '" ~ pak.name ~ "'");
// print("'$filename' in package '$(pak.name)' at '$(pak.path)'");
return (fileInPackage, pak);
}
// print(filename ~ " not in package '" ~ pak.name ~ "'");
// print("'$filename' not in package '$(pak.name)' at '$(pak.path)'");
for (dep in pak.deps) {
string fileInDep = dep.path ~ "/" ~ filename;
string fileInDep = dep.path.sub(filename);

if (exists(fileInDep)) {
// print(filename ~ " in dep package '" ~ dep.name ~ "' of '" ~ pak.name ~ "'");
// print("'$filename' in dep package '$(dep.name)' of '$(pak.name)' (at '$(dep.path)')");
return (fileInDep, dep);
}
// print(filename ~ " not in dep package '" ~ dep.name ~ "' of '" ~ pak.name ~ "'");
// print("'$filename' not in dep package '$(dep.name)' of '$(pak.name)' (at '$(dep.path)')");
}
return locRange.fail("cannot find file '$filename' in package '$(pak.name)' or dependencies");
}

// TODO rewrite this once we have a std.path
(string, Package) findResidualFilename(string filename, Package[] packages)
{
version (windows) import helpers : realpath;
else import package(compiler).std.file : realpath;
import package(compiler).std.file : realpath;

// TODO split then check
if (!exists(filename)) {
Expand Down
21 changes: 17 additions & 4 deletions src/std/file.nt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,23 @@ unittest {
assert("foo/bar.txt".basename == "bar.txt");
}

version (windows) {
} else {
/// The canonicalized absolute pathname of the parameter.
string realpath(string path) {
/// The canonicalized absolute pathname of the parameter.
string realpath(string path) {
version (windows) {
import c.stdlib : exit;
import c.windows : _fullpath, MAX_PATH;

auto ptr = path.toStringz;
auto ret = _fullpath(null, ptr, MAX_PATH);

if (ret is null) {
exit(1);
}
auto ret = ret.cToStringFree;

free(ptr);
return ret;
} else {
import c.stdlib : realpath;

auto ptr = path.toStringz;
Expand Down
1 change: 1 addition & 0 deletions src/std/json.nt
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ private string quote(string s) {
mut string quoted;
for (ch in s) {
if (ch == '"') quoted ~= "\\\"";
else if (ch == '\\') quoted ~= "\\\\";
else quoted ~= ch;
}
return "\"" ~ quoted ~ "\"";
Expand Down

0 comments on commit 68d1975

Please sign in to comment.