From 9808680413aca6f86f37eceda81e98e2b3cffdb6 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Mon, 13 Aug 2018 17:40:55 -0400 Subject: [PATCH] Add stuff. --- lib/metabuild.dst | 63 +++++++++++++++++++++++++++++++++++++---- natives/hello/build.bat | 56 +++++++++++------------------------- src/core/core.dst | 2 +- 3 files changed, 74 insertions(+), 47 deletions(-) diff --git a/lib/metabuild.dst b/lib/metabuild.dst index 1296801f..41d7837f 100644 --- a/lib/metabuild.dst +++ b/lib/metabuild.dst @@ -1,9 +1,14 @@ # A script to help generate build files on different platforms -(def- make-cflags "-std=c99 -Wall -Wextra -O2 -shared -fpic") -(def- make-ldflags "") +# The Makefile generator -(defn $ +(def- make-cflags @["-std=c99" "-Wall" "-Wextra" "-O2" "-shared" "-fpic"]) +(def- make-ldflags @[]) + +(if (= :macos (os.which)) + (array.push make-cflags "undefined -dynamic_lookup")) + +(defn- $ "Do and get the value of a subshell command" [command] (def f (file.popen command)) @@ -25,7 +30,7 @@ "\n\n") out) -(defn generate-make +(defn- generate-make "Generate a makefile" [out-path sources target] (def out (file.open out-path :w)) @@ -35,8 +40,8 @@ out "# Autogenerated Makefile, do not edit\n" "# Generated at " ($ `date`) - "\nCFLAGS:=" make-cflags - "\nLDFLAGS:=" make-ldflags + "\nCFLAGS:=" (string.join make-cflags " ") + "\nLDFLAGS:=" (string.join make-ldflags " ") "\nSOURCES:=" (string.join csources " ") "\nHEADERS:=" (string.join hsources " ") "\nOBJECTS:=$(patsubst %.c,%.o,${SOURCES})" @@ -50,3 +55,49 @@ # Phony targets (emit-rule out ".PHONY" @["all" "clean"]) nil) + +# The batch script generator (windows) + +(defn- batch-crule + "Create a snippet to compile a single C file to an .obj file" + [file-path] + (string + `cl /nologo /I..\..\src\include /c /O2 /W3 ` + file-path + "\n@if errorlevel 1 goto :BUILDFAIL")) + +(defn- generate-batch + "Generate a batch file for windows" + [out-path sources target] + (def out (file.open out-path :w)) + (def csources (filter (fn [x] (= ".c" (string.slice x -2))) sources)) + (file.write + out + "@rem Generated batch script, run in 'Visual Studio Developer Prompt'\n" + "\n@rem \n\n" + "@echo off\n\n" + (string.join (map batch-crule csources) "\n\n") + "\n\n" + `link /nologo /dll ..\..\dst.lib /out:` target `.dll *.obj` + "\nif errorlevel 1 goto :BUILDFAIL" + "\n\n@echo .\n@echo ======\n@echo Build Succeeded.\n@echo =====\n" + "exit /b 0\n\n" + ":BUILDFAIL\n" + "@echo .\n" + "@echo =====\n" + "@echo BUILD FAILED. See Output For Details.\n" + "@echo =====\n" + "@echo .\n" + "exit /b 1\n") + (file.flush out) + (file.close out) + nil) + +(defn generate + "Generate the build files for a given library." + [out-path sources target] + ((if (= :windows (os.which)) generate-batch generate-make) + out-path + sources + target)) + diff --git a/natives/hello/build.bat b/natives/hello/build.bat index ed68d570..873b204f 100644 --- a/natives/hello/build.bat +++ b/natives/hello/build.bat @@ -1,49 +1,25 @@ -@rem Build dst on windows -@rem -@rem Open a "Windows SDK Command Shell" and cd to the dst directory -@rem Then run this script with no arguments to build the executable +@rem Generated batch script, run in 'Visual Studio Developer Prompt' + +@rem @echo off -@rem Ensure correct command prompt -@if not defined INCLUDE goto :BADCMD - -@rem Subcommands -@if "%1"=="clean" goto CLEAN - -@rem Set compile and link options here -@setlocal -@set DST_COMPILE=cl /nologo /I..\..\src\include /c /O2 /W3 -@set DST_LINK=link /nologo /dll ..\..\dst.lib - -@rem Build the sources -for %%f in (*.c) do ( - @%DST_COMPILE% %%f - @if errorlevel 1 goto :BUILDFAIL -) - -%DST_LINK% /out:hello.dll *.obj +cl /nologo /I..\..\src\include /c /O2 /W3 hello.c @if errorlevel 1 goto :BUILDFAIL -echo === Successfully built hello.dll for Windows === -echo === Run 'build clean' to delete build artifacts. === +link /nologo /dll ..\..\dst.lib /out:hello.dll *.obj +if errorlevel 1 goto :BUILDFAIL + +@echo . +@echo ====== +@echo Build Succeeded. +@echo ===== exit /b 0 -@rem Not using correct command line -:BADCMD -@echo Use a Visual Studio Developer Command Prompt to run this script -exit /b 1 - -@rem Clean build artifacts -:CLEAN -del *.obj -del hello.* -exit /b 0 - -@rem Build failed :BUILDFAIL -@echo. -@echo ******************************************************* -@echo *** Build FAILED -- Please check the error messages *** -@echo ******************************************************* +@echo . +@echo ===== +@echo BUILD FAILED. See Output For Details. +@echo ===== +@echo . exit /b 1 diff --git a/src/core/core.dst b/src/core/core.dst index 2cd5d9db..3dbef6a5 100644 --- a/src/core/core.dst +++ b/src/core/core.dst @@ -1080,7 +1080,7 @@ env) (defn default-error-handler - [source t x f] + @[source t x f] (file.write stdout (string t " error in " source ": ")) (if (bytes? x) (do (file.write stdout x)