SConstruct (4679B)
1 #!/usr/bin/env python 2 import os 3 import sys 4 5 # Try to detect the host platform automatically. 6 # This is used if no `platform` argument is passed 7 if sys.platform.startswith("linux"): 8 host_platform = "linux" 9 elif sys.platform == "darwin": 10 host_platform = "osx" 11 elif sys.platform == "win32" or sys.platform == "msys": 12 host_platform = "windows" 13 else: 14 raise ValueError( 15 "Could not detect platform automatically, please specify with " 16 "platform=<platform>" 17 ) 18 19 env = Environment(ENV=os.environ) 20 21 is64 = sys.maxsize > 2 ** 32 22 if (env["TARGET_ARCH"] == "amd64" 23 or env["TARGET_ARCH"] == "emt64" 24 or env["TARGET_ARCH"] == "x86_64" 25 or env["TARGET_ARCH"] == "arm64-v8a"): 26 is64 = True 27 28 opts = Variables([], ARGUMENTS) 29 30 opts.Add( 31 EnumVariable( 32 "platform", 33 "Target platform", 34 host_platform, 35 allowed_values=("linux", "osx", "windows"), 36 ignorecase=2, 37 ) 38 ) 39 40 opts.Add( 41 EnumVariable( 42 "target", 43 "Compilation target", 44 "release", 45 allowed_values=("debug", "release"), 46 ignorecase=2, 47 ) 48 ) 49 opts.Add( 50 PathVariable("target_path", "The path where the app will be installed", "../../bin/") 51 ) 52 53 opts.Add( 54 PathVariable("source_path", "The path where the source files lay", "../../source/") 55 ) 56 57 opts.Add( 58 PathVariable("include_path", "Where your includes are", "../../third_party/include") 59 ) 60 61 opts.Add( 62 PathVariable("target_name", "The name of the app", "App", PathVariable.PathAccept) 63 ) 64 65 opts.Add( 66 EnumVariable("bits", "Target platform bits", "64" if is64 else "32", ("32", "64")) 67 ) 68 69 opts.Add( 70 EnumVariable( 71 "OpenGL", 72 "If using OpenGL as backend", 73 "false", 74 allowed_values=("false", "true"), 75 ignorecase=2, 76 ) 77 ) 78 79 # Create bin folder if it does not exist, it will throw an error 80 # if any of the paths above is not an actual path. Since the project 81 # template does not actually have the bin folder, it won't work if you use 82 # the default unless you create it manually. 83 # 84 # feel free to remove this if you don't want the bin folder 85 os.system("mkdir -p ../../bin/") 86 87 # update options 88 opts.Update(env) 89 90 91 if host_platform == "windows": 92 env.Append(ENV=os.environ) 93 if env["bits"] == "64": 94 env = Environment(TARGET_ARCH="amd64") 95 elif env["bits"] == "32": 96 env = Environment(TARGET_ARCH="x86") 97 opts.Update(env) 98 99 env.Append(CPPPATH=env['include_path']) 100 101 if env["platform"] == "linux": 102 if env["target"] == "debug": 103 env.Append(CCFLAGS=["-fPIC", "-g3", "-Og", "-std=gnu99"]) 104 else: 105 env.Append(CCFLAGS=["-fPIC", "-O3", "-std=gnu99"]) 106 env.Append(LINKFLAGS=["-s"]) 107 108 env.Append(LINKFLAGS=["-ldl", "-lX11", "-lXi", "-lm", "-pthread"]) 109 if env["OpenGL"] == "true": 110 env.Append(CCFLAGS=["-lGL"]) 111 112 elif env["platform"] == "osx": 113 if env["target"] == "debug": 114 env.Append(CCFLAGS=["-g", "-O2", "-arch", "x86_64", "-std=gnu99", "-objective-c"]) 115 else: 116 env.Append(CCFLAGS=["-g", "-O3", "-arch", "x86_64", "-std=gnu99", "-objective-c"]) 117 118 env.Append(LINKFLAGS=["-arch", "x86_64", "-framework", "CoreFoundation", "-framework", 119 "CoreVide", "-framework", "IOKit", "-framework", "Cocoa", "-framework", "Carbon"]) 120 if env["OpenGL"] == "true": 121 env.Append(CCFLAGS=["-framework", "OpenGL"]) 122 123 elif env["platform"] == "windows": 124 env.Append(CPPDEFINES=["WIN32", "_WIN32", "_WINDOWS", "_CRT_SECURE_NO_WARNINGS"]) 125 if env["target"] == "debug": 126 env.Append(CPPDEFINES=["_DEBUG"]) 127 env.Append(CCFLAGS=["-MD", "-std=gnu99"]) 128 env.Append(LINKFLAGS=["-DEBUG"]) 129 else: 130 env.Append(CPPDEFINES=["NDEBUG"]) 131 env.Append(CCFLAGS=["-O2", "-MD", "-std=gnu99"]) 132 133 if env["bits"] == "32" and host_platform == "windows": 134 env.Append(LINKFLAGS=["kernel32.lib", "user32.lib", "shell32.lib", "vcruntime.lib", "msvcrt.lib", "gdi32.lib", "Advapi32.lib", "winmm.lib"]) 135 if env["OpenGL"] == "true": 136 env.Append(LINKFLAGS=["opengl32.lib"]) 137 else: 138 env.Append(LINKFLAGS=["-mwindows", "-lkernel32", "-luser32", "-lshell32", "-lgdi32", "-lAdvapi32", "-lwinmm"]) 139 if env["OpenGL"] == "true": 140 env.Append(LINKFLAGS=["-lopengl32"]) 141 142 143 # Source Files 144 sources = Glob(f"{env['source_path']}/*.c") 145 sources += Glob(f"{env['source_path']}/*/*.c") 146 sources += Glob(f"{env['source_path']}/*.cpp") 147 sources += Glob(f"{env['source_path']}/*/*.cpp") 148 149 app = env.Program( 150 target=f"{env['target_path']}{env['platform']}/{env['target_name']}", source=sources 151 ) 152 Default(app) 153 154 # Generates help for the -h scons option. 155 Help(opts.GenerateHelpText(env))