From 90c4554c7e9bac630413a4b834e15afa8fa2f331 Mon Sep 17 00:00:00 2001 From: Joey Yakimowich-Payne Date: Tue, 28 Apr 2020 19:19:34 -0600 Subject: [PATCH] WIP Windows support --- .gitignore | 1 + src/sdl2/sdl2.nim | 98 ++++++++++++++++++++++++++++++++++++----- src/sdl2/sdl2_image.nim | 11 +++-- src/sdl2/sdl2_mixer.nim | 16 ++++--- src/sdl2/sdl2_net.nim | 11 +++-- src/sdl2/sdl2_ttf.nim | 17 ++++--- 6 files changed, 126 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 838faf3..8baf078 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ build *.out +*.exe .DS_Store \ No newline at end of file diff --git a/src/sdl2/sdl2.nim b/src/sdl2/sdl2.nim index 5ef6513..3288788 100644 --- a/src/sdl2/sdl2.nim +++ b/src/sdl2/sdl2.nim @@ -6,13 +6,20 @@ const baseDir = SDLCacheDir srcDir = baseDir / "sdl2" buildDir = srcDir / "buildcache" + includeDir = srcDir / "include" symbolPluginPath = currentSourcePath.parentDir() / "cleansymbols.nim" +when defined(windows): + const dlurl = "https://www.libsdl.org/release/SDL2-$1.zip" +else: + const dlurl = "https://www.libsdl.org/release/SDL2-$1.tar.gz" + getHeader( "SDL.h", - dlurl = "https://www.libsdl.org/release/SDL2-$1.tar.gz", + dlurl = dlurl, outdir = srcDir, - cmakeFlags = "-DSDL_STATIC_PIC=ON" + cmakeFlags = "-DSDL_STATIC_PIC=ON", + altNames = "SDL2" ) # {.passL: "-framework GLUT -framework OpenGL -framework Cocoa -framework IOKit -framework CoreVideo -framework Carbon -framework CoreAudio -lm".} @@ -20,7 +27,7 @@ static: cSkipSymbol @[ "bool", "compile_time_assert_SDL_Event", "INLINE", "FUNCTION", "FILE", "LINE", "PRIu64", "PRIx64", "SDLPRIX64", "assert_state", - "assert_data", + "assert_data", "NULL" # "SDL_AssertState", "SDL_AssertData", "SDL_threadID", "SDL_Keycode", "SDL_HapticConstant", "SDL_HapticRamp", "SDL_HapticLeftRight", "SDL_HapticCustom", "SDL_HapticPause", "SDL_Log" ] # cDebug() @@ -99,16 +106,59 @@ cOverride: cPluginPath(symbolPluginPath) static: - when SDL_Static: - const conf = staticExec(&"cd {srcDir}; ./sdl2-config --static-libs || (./configure --silent; ./sdl2-config --static-libs)").replace("-lSDL2 ", "").split('\n')[^1] + # when SDL_Static: + # when defined(windows): + # const conf = "-lmingw32 -mwindows -Wl,--no-undefined -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid -static-libgcc" + # else: + # const conf = staticExec(&"cd {srcDir}; ./sdl2-config --static-libs").replace("-lSDL2 ", "").replace("-lSDL2main", "").split('\n')[^1] + # else: + # when defined(windows): + # const conf = "-lmingw32 -lSDL2main -lSDL2 -mwindows" + # else: + # const conf = staticExec(&"cd {srcDir}; ./sdl2-config --libs || (./configure --silent; ./sdl2-config --libs)").split('\n')[^1] + + when defined(windows): + when defined(amd64): + const flags = "--host=x86_64-w64-mingw32" + else: + const flags = "--host=i686-w64-mingw32" else: - const conf = staticExec(&"cd {srcDir}; ./sdl2-config --libs || (./configure --silent; ./sdl2-config --libs)").split('\n')[^1] - {.passL: conf.} + const flags = "" + configure(srcDir.sanitizePath, "sdl2-config", flags) + +proc unixizePath*(path: string, noQuote = false, sep = $DirSep): string = + result = path.multiReplace([("C:\\", "/c/"), (sep, "/")]) + if not noQuote: + result = result.quoteShell + +static: + proc getLibOutput(output: string): string = + for line in output.split('\l'): + if "lSDL2" in line: + return line.strip()#.replace("-lSDL2 ", "").replace("-lSDL2main ", "") + + when defined(SDL_Static): + when defined(windows): + #TODO: Find a way to automate this on Windows reliably + const conf = "-lmingw32 -lSDL2main -lSDL2 -mwindows -Wl,--no-undefined -Wl,--dynamicbase -Wl,--nxcompat -Wl,--high-entropy-va -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid -static-libgcc" + else: + const cmd = &"cd {srcDir.sanitizePath} && bash ./sdl2-config --static-libs" + const conf = execAction(cmd).output.getLibOutput + else: + when defined(windows): + #TODO: Find a way to automate this on Windows reliably + const conf = "-lmingw32 -lSDL2main -lSDL2 -mwindows" + else: + const cmd = &"cd {srcDir.sanitizePath} && bash ./sdl2-config --libs" + const conf = execAction(cmd).output.getLibOutput + echo conf + echo buildDir.unixizePath + {.passL: &"-L{buildDir.sanitizePath} -L{buildDir.unixizePath} {conf}".} when defined(SDL_Static): - cImport(SDL_Path, recurse = true, flags = "-f=ast2 -DDOXYGEN_SHOULD_IGNORE_THIS -E__,_ -F__,_") + cImport(srcDir/"include"/"SDL.h", recurse = true, flags = "-f=ast2 -DDOXYGEN_SHOULD_IGNORE_THIS -E__,_ -F__,_") else: - cImport(SDL_Path, recurse = true, dynlib = "SDL_LPath", flags = "-f=ast2 -DDOXYGEN_SHOULD_IGNORE_THIS -E__,_ -F__,_") + cImport(srcDir/"include"/"SDL.h", recurse = true, dynlib = "SDL_LPath", flags = "-f=ast2 -DDOXYGEN_SHOULD_IGNORE_THIS -E__,_ -F__,_") proc getDynlibExt(): string = when defined(windows): @@ -122,9 +172,35 @@ proc findDynlib(): string = const pathRegex = "(lib)?SDL2[_-]?(static)?[0-9.\\-]*\\" & getDynlibExt() return findFile(pathRegex, buildDir, regex = true) +proc findStaticlib(): string = + const pathRegex = "(lib)?SDL2[_-]?(static)?[0-9.\\-]*\\.a" + return findFile(pathRegex, buildDir, regex = true) + +proc findStaticMainlib(): string = + const pathRegex = "(lib)?SDL2[_-]?(static)?[0-9.\\-]*\\main.a" + return findFile(pathRegex, buildDir, regex = true) + const SDLDyLibPath* = findDynlib() -const SDLMainLib* = buildDir / "libSDL2main.a" -const SDLStaticLib* = buildDir / "libSDL2.a" +const SDLMainLib* = buildDir / findStaticMainlib() +const SDLStaticLib* = buildDir / findStaticlib() +const SDL2ConfigPath* = srcDir / "sdl2-config" +const SDLSrcDir* = srcDir + +static: + let pathenv = getEnv("PATH") + when defined(windows): + putEnv("PATH", buildDir.sanitizePath & ";" & pathenv) + else: + putEnv("PATH", buildDir.sanitizePath & ":" & pathenv) + echo pathenv + echo includeDir + putEnv("CFLAGS", "-I" & includeDir.sanitizePath) + putEnv("LDFLAGS", &"-L{buildDir.sanitizePath} -L{buildDir.unixizePath}") # & " " & buildDir/"libSDL2main.a") + putEnv("SDL2_CONFIG", (srcDir/"sdl2-config").sanitizePath) + putEnv("LIBS", &"-L{buildDir.sanitizePath} -L{buildDir.unixizePath}") + putEnv("SDL_CFLAGS", "-I" & includeDir.sanitizePath) + putEnv("LD_LIBRARY_PATH", &"{buildDir.sanitizePath}:{buildDir.unixizePath}") + putEnv("SDL2_PATH", srcDir.sanitizePath) const WINDOWPOS_UNDEFINED* = WINDOWPOS_UNDEFINED_DISPLAY(0) diff --git a/src/sdl2/sdl2_image.nim b/src/sdl2/sdl2_image.nim index a48f503..8b65e12 100644 --- a/src/sdl2/sdl2_image.nim +++ b/src/sdl2/sdl2_image.nim @@ -8,9 +8,14 @@ const srcDir = baseDir / "sdl2_image" symbolPluginPath = currentSourcePath.parentDir() / "cleansymbols.nim" +when defined(windows): + const dlurl = "https://www.libsdl.org/projects/SDL_image/release/SDL2_image-$1.zip" +else: + const dlurl = "https://www.libsdl.org/projects/SDL_image/release/SDL2_image-$1.tar.gz" + getHeader( "SDL_image.h", - dlurl = "https://www.libsdl.org/projects/SDL_image/release/SDL2_image-$1.tar.gz", + dlurl = dlurl, outdir = srcDir, altNames = "SDL2_image" ) @@ -28,6 +33,6 @@ cOverride: cPluginPath(symbolPluginPath) when defined(SDL_image_Static): - cImport(SDL_image_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir/"SDL_image.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") else: - cImport(SDL_image_Path, recurse = false, dynlib = "SDL_image_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir/"SDL_image.h", recurse = false, dynlib = "SDL_image_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") diff --git a/src/sdl2/sdl2_mixer.nim b/src/sdl2/sdl2_mixer.nim index 13cf642..67e734c 100644 --- a/src/sdl2/sdl2_mixer.nim +++ b/src/sdl2/sdl2_mixer.nim @@ -5,14 +5,20 @@ import nimterop/[cimport, build] const baseDir = SDLCacheDir sdlIncludeDir = baseDir / "sdl2" / "include" + sdlBuildDir = baseDir / "sdl2" / "buildcache" srcDir = baseDir / "sdl2_mixer" symbolPluginPath = currentSourcePath.parentDir() / "cleansymbols.nim" +when defined(windows): + const dlurl = "https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-$1.zip" +else: + const dlurl = "https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-$1.tar.gz" + getHeader( "SDL_mixer.h", - dlurl = "https://www.libsdl.org/projects/SDL_mixer/release/SDL2_mixer-$1.tar.gz", + dlurl = dlurl, outdir = srcDir, - altNames = "SDL2_mixer" + altNames = "SDL2_mixer", ) # static: @@ -27,7 +33,7 @@ cOverride: cPluginPath(symbolPluginPath) -when defined(SDL_mixer_Static): - cImport(SDL_mixer_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") +when defined(SDL_Mixer_Static): + cImport(srcDir/"SDL_mixer.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") else: - cImport(SDL_mixer_Path, recurse = false, dynlib = "SDL_mixer_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir/"SDL_mixer.h", recurse = false, dynlib = "SDL_mixer_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") diff --git a/src/sdl2/sdl2_net.nim b/src/sdl2/sdl2_net.nim index cf2fdf1..785e99c 100644 --- a/src/sdl2/sdl2_net.nim +++ b/src/sdl2/sdl2_net.nim @@ -8,9 +8,14 @@ const srcDir = baseDir / "sdl2_net" symbolPluginPath = currentSourcePath.parentDir() / "cleansymbols.nim" +when defined(windows): + const dlurl = "https://www.libsdl.org/projects/SDL_net/release/SDL2_net-$1.zip" +else: + const dlurl = "https://www.libsdl.org/projects/SDL_net/release/SDL2_net-$1.tar.gz" + getHeader( "SDL_net.h", - dlurl = "https://www.libsdl.org/projects/SDL_net/release/SDL2_net-$1.tar.gz", + dlurl = dlurl, outdir = srcDir, altNames = "SDL2_net" ) @@ -26,8 +31,8 @@ cOverride: cPluginPath(symbolPluginPath) when defined(SDL_net_Static): - cImport(SDL_net_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir / "SDL_net.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") cImport(srcDir / "SDLnetsys.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") else: - cImport(SDL_net_Path, recurse = false, dynlib = "SDL_net_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir / "SDL_net.h", recurse = false, dynlib = "SDL_net_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") cImport(srcDir / "SDLnetsys.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2") \ No newline at end of file diff --git a/src/sdl2/sdl2_ttf.nim b/src/sdl2/sdl2_ttf.nim index 3337379..d9521f5 100644 --- a/src/sdl2/sdl2_ttf.nim +++ b/src/sdl2/sdl2_ttf.nim @@ -8,16 +8,21 @@ const sdlIncludeDir = sdlDir / "include" srcDir = baseDir / "sdl2_ttf" currentPath = currentSourcePath().parentDir().parentDir() - cmakeModPath = currentPath / "cmake" / "sdl2" + cmakeModPath = (currentPath / "cmake" / "sdl2").replace("\\", "/") symbolPluginPath = currentPath / "sdl2" / "cleansymbols.nim" +when defined(windows): + const dlurl = "https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-$1.zip" +else: + const dlurl = "https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-$1.tar.gz" + getHeader( "SDL_ttf.h", - dlurl = "https://www.libsdl.org/projects/SDL_ttf/release/SDL2_ttf-$1.tar.gz", + dlurl = dlurl, outdir = srcDir, altNames = "SDL2_ttf", - cmakeFlags = &"-DCMAKE_C_FLAGS=-I{sdlIncludeDir} -DCMAKE_MODULE_PATH={cmakeModPath} " & - &"-DSDL2MAIN_LIBRARY={SDLMainLib} -DSDL2_LIBRARY={SDLDyLibPath} -DSDL2_PATH={sdlDir}" + cmakeFlags = &"-DCMAKE_C_FLAGS=-I{sdlIncludeDir.sanitizePath} -DCMAKE_MODULE_PATH={cmakeModPath} " & + &"-DSDL2MAIN_LIBRARY={SDLMainLib.sanitizePath} -DSDL2_LIBRARY={SDLDyLibPath.sanitizePath} -DSDL2_PATH={sdlDir.sanitizePath}" ) # static: @@ -32,6 +37,6 @@ cOverride: SetError* = "" when defined(SDL_ttf_Static): - cImport(SDL_ttf_Path, recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2 -d") + cImport(srcDir / "SDL_ttf.h", recurse = false, flags = &"-I={sdlIncludeDir} -f=ast2 -d") else: - cImport(SDL_ttf_Path, recurse = false, dynlib = "SDL_ttf_LPath", flags = &"-I={sdlIncludeDir} -f=ast2") + cImport(srcDir / "SDL_ttf.h", recurse = false, dynlib = "SDL_ttf_LPath", flags = &"-I={sdlIncludeDir} -f=ast2")