| 1 |
print("Using GNU tools configuration") |
| 2 |
|
| 3 |
Import('defenv') |
| 4 |
|
| 5 |
### imports |
| 6 |
|
| 7 |
Import('FlagsConfigure GetOptionOrEnv GetStdSysEnvVarList') |
| 8 |
|
| 9 |
### HACKS! |
| 10 |
if GetOptionOrEnv('NSIS_SCONS_GNU_ENVPATHHACK'): |
| 11 |
import os |
| 12 |
defenv['ENV']['PATH'] = os.getenv('PATH') # Major hack! |
| 13 |
import_env = GetStdSysEnvVarList(path=True, temp=True) |
| 14 |
for var in import_env: |
| 15 |
if var in os.environ: |
| 16 |
defenv['ENV'][var] = os.environ.get(var, '') |
| 17 |
#print(defenv.Dump()) |
| 18 |
|
| 19 |
### cross compiling |
| 20 |
|
| 21 |
def cross_env(env): |
| 22 |
if env['PLATFORM'] != 'win32': |
| 23 |
env.Tool('crossmingw', toolpath = [Dir('../Tools').rdir()]) |
| 24 |
|
| 25 |
### flags |
| 26 |
|
| 27 |
code_failonmswin = """ |
| 28 |
#ifdef _WIN32 |
| 29 |
#error Not supported on Windows |
| 30 |
#endif |
| 31 |
""" |
| 32 |
|
| 33 |
def entry(x,u): |
| 34 |
if defenv['TARGET_ARCH'] == 'x86': |
| 35 |
if x == 'NSISWinMainNOCRT': |
| 36 |
x = '_' + x |
| 37 |
elif x == 'DllMain': |
| 38 |
x = '_DllMain@12' |
| 39 |
return '-Wl,-e%s' % x |
| 40 |
|
| 41 |
defenv['ENTRY_FLAG'] = entry |
| 42 |
defenv['MAP_FLAG'] = '-Wl,-Map,${TARGET.base}.map' |
| 43 |
defenv['EXCEPTION_FLAG'] = '' |
| 44 |
defenv['NODEFLIBS_FLAG'] = '-nostdlib -Wl,--exclude-libs,msvcrt.a' |
| 45 |
defenv['C_FLAG'] = '-xc' |
| 46 |
defenv['CPP_FLAG'] = '-xc++' |
| 47 |
defenv['ALIGN_FLAG'] = '-Wl,--file-alignment,512' |
| 48 |
defenv['CPP_REQUIRES_STDLIB'] = 1 |
| 49 |
defenv['SUBSYS_CON'] = '-Wl,--subsystem,console' |
| 50 |
defenv['SUBSYS_WIN'] = '-Wl,--subsystem,windows' |
| 51 |
defenv['MSVCRT_FLAG'] = '' |
| 52 |
defenv['STDCALL'] = '"__attribute__((__stdcall__))"' |
| 53 |
|
| 54 |
# Don't allow mingw to link with LIBGCC*.DLL and LIBSTDC++-*.DLL |
| 55 |
def configure_static_libs(env): |
| 56 |
env.Append(LINKFLAGS = ['-static-libgcc']) |
| 57 |
env.Append(LINKFLAGS = ['-static-libstdc++']) # MinGW GCC 4.5.2 warns about unrecognized option but it also actually needs it! |
| 58 |
|
| 59 |
if defenv['PLATFORM'] == 'win32': |
| 60 |
configure_static_libs(defenv) |
| 61 |
|
| 62 |
### defines |
| 63 |
|
| 64 |
defenv.Append(CPPDEFINES = [('NSISCALL', '$STDCALL')]) |
| 65 |
|
| 66 |
### helper functions |
| 67 |
|
| 68 |
# on Mac OS X, programs built with g++ 4.0, stl and -s error out: |
| 69 |
# dyld: lazy symbol binding failed: lazy pointer not found |
| 70 |
# dyld: lazy pointer not found |
| 71 |
# |
| 72 |
# to avoid this, this function checks if -s works |
| 73 |
|
| 74 |
def TestStrip(ctx): |
| 75 |
c = """ |
| 76 |
#include <vector> |
| 77 |
|
| 78 |
int main() { |
| 79 |
std::vector<int> v; |
| 80 |
return 0; |
| 81 |
} |
| 82 |
""" |
| 83 |
ctx.CheckLinkFlag('-s', run = 1, extension = '.cpp', code = c) |
| 84 |
|
| 85 |
### debug |
| 86 |
|
| 87 |
if defenv['DEBUG']: |
| 88 |
defenv.Append(CCFLAGS = '-g') |
| 89 |
|
| 90 |
### unicode |
| 91 |
tdefenv = defenv.Clone() |
| 92 |
if tdefenv['UNICODE']: |
| 93 |
tdefenv.Append(CPPDEFINES = ['_UNICODE', 'UNICODE']) |
| 94 |
|
| 95 |
### stub environment |
| 96 |
|
| 97 |
stub_env = defenv.Clone() |
| 98 |
cross_env(stub_env) |
| 99 |
|
| 100 |
stub_env.Append(CPPPATH = ['#$BUILD_CONFIG']) |
| 101 |
|
| 102 |
if not defenv['DEBUG']: |
| 103 |
stub_env.Append(CCFLAGS = ['-Os']) # optimize for size |
| 104 |
stub_env.Append(CCFLAGS = ['-Wall']) # all warnings |
| 105 |
stub_env.Append(CCFLAGS = ['-xc']) # force compile as c |
| 106 |
stub_env.Append(CCFLAGS = ['-fno-strict-aliasing']) # not safe for strict aliasing |
| 107 |
|
| 108 |
if not defenv['DEBUG'] and defenv['STRIP'] and defenv['STRIP_W32']: |
| 109 |
stub_env.Append(LINKFLAGS = ['-s']) # strip |
| 110 |
stub_env.Append(LINKFLAGS = ['-mwindows']) # build windows executables |
| 111 |
stub_env.Append(LINKFLAGS = ['$ALIGN_FLAG']) # 512 bytes align |
| 112 |
stub_env.Append(LINKFLAGS = ['$MAP_FLAG']) # generate map file |
| 113 |
conf = FlagsConfigure(stub_env) |
| 114 |
conf.CheckCompileFlag('-fno-tree-loop-distribute-patterns') # GCC 10: Don't generate msvcrt!memmove calls (bug #1248) |
| 115 |
conf.CheckLinkFlag('-Wl,--disable-reloc-section') # binutils 2.36, ld will include a .reloc section by default (bug #1283) |
| 116 |
conf.Finish() |
| 117 |
stub_env.Append(LINKFLAGS = ['$NODEFLIBS_FLAG']) # no standard libraries |
| 118 |
|
| 119 |
stub_uenv = stub_env.Clone() |
| 120 |
stub_uenv.Append(CPPDEFINES = ['_UNICODE', 'UNICODE']) |
| 121 |
|
| 122 |
### makensis environment |
| 123 |
|
| 124 |
makensis_env = tdefenv.Clone() |
| 125 |
|
| 126 |
makensis_env.Append(CPPPATH = ['#$BUILD_CONFIG']) |
| 127 |
|
| 128 |
if not defenv['DEBUG']: |
| 129 |
makensis_env.Append(CCFLAGS = ['-O2']) # optimize |
| 130 |
makensis_env.Append(CFLAGS = ['-Wall']) # all warnings |
| 131 |
makensis_env.Append(CXXFLAGS = ['-Wno-non-virtual-dtor']) # ignore virtual dtor warnings |
| 132 |
makensis_env.Append(CXXFLAGS = ['-Wall']) # all warnings |
| 133 |
makensis_env['STDCALL'] = '' # avoid warnings |
| 134 |
|
| 135 |
conf = FlagsConfigure(makensis_env) |
| 136 |
conf.CheckLinkFlag('$MAP_FLAG') # generate map file |
| 137 |
if not defenv['DEBUG'] and defenv['STRIP'] and defenv['STRIP_CP']: |
| 138 |
TestStrip(conf) # strip |
| 139 |
conf.Finish() |
| 140 |
|
| 141 |
### plugin environment |
| 142 |
|
| 143 |
plugin_env = defenv.Clone() |
| 144 |
cross_env(plugin_env) |
| 145 |
|
| 146 |
if not defenv['DEBUG']: |
| 147 |
plugin_env.Append(CCFLAGS = ['-Os']) # optimize for size |
| 148 |
plugin_env.Append(CCFLAGS = ['-Wall']) # level 3 warnings |
| 149 |
plugin_env.Append(CCFLAGS = ['-fno-strict-aliasing']) # not safe for strict aliasing |
| 150 |
|
| 151 |
if not defenv['DEBUG'] and defenv['STRIP'] and defenv['STRIP_W32']: |
| 152 |
plugin_env.Append(LINKFLAGS = ['-s']) # strip |
| 153 |
plugin_env.Append(LINKFLAGS = ['-mwindows']) # build windows executables |
| 154 |
plugin_env.Append(LINKFLAGS = ['$ALIGN_FLAG']) # 512 bytes align |
| 155 |
plugin_env.Append(LINKFLAGS = ['$MAP_FLAG']) # generate map file |
| 156 |
configure_static_libs(plugin_env) # remove libgcc*.dll & libstdc++*.dll dependency |
| 157 |
|
| 158 |
plugin_uenv = plugin_env.Clone() |
| 159 |
plugin_uenv.Append(CPPDEFINES = ['_UNICODE', 'UNICODE']) |
| 160 |
|
| 161 |
### cross-platform util environment |
| 162 |
|
| 163 |
if defenv['PLATFORM'] == 'win32': |
| 164 |
cp_util_env = tdefenv.Clone() |
| 165 |
else: |
| 166 |
cp_util_env = defenv.Clone() |
| 167 |
|
| 168 |
cp_util_env.Append(CPPPATH = ['#$BUILD_CONFIG']) |
| 169 |
|
| 170 |
if cp_util_env['PLATFORM'] == 'win32': |
| 171 |
cp_util_env.Append(LINKFLAGS = ['$ALIGN_FLAG']) # 512 bytes align |
| 172 |
|
| 173 |
if not defenv['DEBUG']: |
| 174 |
cp_util_env.Append(CCFLAGS = ['-O2']) # optimize |
| 175 |
cp_util_env.Append(CCFLAGS = ['-Wall']) # all warnings |
| 176 |
cp_util_env.Append(CCFLAGS = ['-fno-strict-aliasing']) # not safe for strict aliasing |
| 177 |
|
| 178 |
conf = FlagsConfigure(cp_util_env) |
| 179 |
conf.CheckLinkFlag('$MAP_FLAG') # generate map file |
| 180 |
conf.Finish() |
| 181 |
|
| 182 |
### util environment |
| 183 |
|
| 184 |
util_env = tdefenv.Clone() |
| 185 |
cross_env(util_env) |
| 186 |
|
| 187 |
util_env.Append(CPPPATH = ['#$BUILD_CONFIG']) |
| 188 |
|
| 189 |
if not defenv['DEBUG']: |
| 190 |
util_env.Append(CCFLAGS = ['-O2']) # optimize |
| 191 |
util_env.Append(CCFLAGS = ['-Wall']) # all warnings |
| 192 |
util_env.Append(CCFLAGS = ['-fno-strict-aliasing']) # not safe for strict aliasing |
| 193 |
|
| 194 |
util_env.Append(LINKFLAGS = ['-mwindows']) # build windows executables |
| 195 |
util_env.Append(LINKFLAGS = ['$ALIGN_FLAG']) # 512 bytes align |
| 196 |
|
| 197 |
|
| 198 |
conf = FlagsConfigure(util_env) |
| 199 |
if not defenv['DEBUG'] and defenv['STRIP'] and defenv['STRIP_W32']: |
| 200 |
util_env.Append(LINKFLAGS = ['-s']) # strip |
| 201 |
conf.Finish() |
| 202 |
|
| 203 |
### cross-platform util environment adjustments |
| 204 |
|
| 205 |
conf = FlagsConfigure(cp_util_env) |
| 206 |
if not defenv['DEBUG'] and defenv['STRIP'] and defenv['STRIP_CP']: |
| 207 |
TestStrip(conf) # strip |
| 208 |
conf.Finish() |
| 209 |
|
| 210 |
### test environment |
| 211 |
|
| 212 |
test_env = defenv.Clone() |
| 213 |
test_env['STDCALL'] = '' # avoid warnings |
| 214 |
test_env.Append(CPPPATH = ['#$BUILD_CONFIG']) |
| 215 |
conf = FlagsConfigure(test_env) |
| 216 |
conf.Finish() |
| 217 |
|
| 218 |
### weird GCC requirements |
| 219 |
|
| 220 |
# |
| 221 |
# GCC puts new PE sections, added by code, between other sections. |
| 222 |
# This is not good for the .ndata section because makensis changes |
| 223 |
# its size dynamically. This is not good if RVAs to sections below |
| 224 |
# it are saved in other places. The RVAs will point to garbage. |
| 225 |
# |
| 226 |
# To fix this, a linker script is provided. The linker script makes |
| 227 |
# sure the sections will be written in the correct order. |
| 228 |
# |
| 229 |
|
| 230 |
petype = 'pei-i386' |
| 231 |
if defenv['TARGET_ARCH'] == 'amd64': |
| 232 |
petype = 'pei-x86-64' |
| 233 |
stub_env.Append(LINKFLAGS = ['-B', petype]) # --oformat petype also works in GCC 4.5.2 |
| 234 |
stub_uenv.Append(LINKFLAGS = ['-B', petype]) |
| 235 |
stub_env.Append(LINKFLAGS = ['-T', File('linker_script').rfile()]) |
| 236 |
stub_uenv.Append(LINKFLAGS = ['-T', File('linker_script').rfile()]) |
| 237 |
|
| 238 |
|
| 239 |
# |
| 240 |
# GCC requires some functions from the CRT to be present, if certain |
| 241 |
# operations are done. For example, if a small string is assigned to |
| 242 |
# a larger buffer, GCC 3.4+ uses memset to fill the remaining of the |
| 243 |
# buffer with zeros. |
| 244 |
# |
| 245 |
|
| 246 |
def check_requirement(ctx, func, trigger): |
| 247 |
ctx.Message('Checking for %s requirement... ' % func) |
| 248 |
|
| 249 |
flags = ctx.env['LINKFLAGS'] |
| 250 |
ctx.env.Append(LINKFLAGS = ['$NODEFLIBS_FLAG']) |
| 251 |
|
| 252 |
codeprepend = """ |
| 253 |
#define CONFCHECK_CALLFUNC() check |
| 254 |
static int check() { %s } |
| 255 |
""" % trigger |
| 256 |
Import('GenerateTryLinkCode') |
| 257 |
code = GenerateTryLinkCode(codeprepend = codeprepend) |
| 258 |
result = not ctx.TryLink(code, '.c') |
| 259 |
ctx.Result(result) |
| 260 |
|
| 261 |
ctx.env['LINKFLAGS'] = flags |
| 262 |
return result |
| 263 |
|
| 264 |
def add_file_to_emitter(env, emitter_name, file): |
| 265 |
try: |
| 266 |
original_emitter = env[emitter_name] |
| 267 |
if type(original_emitter) == list: |
| 268 |
original_emitter = original_emitter[0] |
| 269 |
except KeyError: |
| 270 |
original_emitter = None |
| 271 |
|
| 272 |
def emitter(target, source, env): |
| 273 |
if original_emitter: |
| 274 |
target, source = original_emitter(target, source, env) |
| 275 |
|
| 276 |
if '$NODEFLIBS_FLAG' not in env['LINKFLAGS']: |
| 277 |
return target, source |
| 278 |
|
| 279 |
return target, source + [file] |
| 280 |
|
| 281 |
env[emitter_name] = emitter |
| 282 |
|
| 283 |
def add_file(file): |
| 284 |
file = File(file) |
| 285 |
add_file_to_emitter(stub_env, 'PROGEMITTER', file) |
| 286 |
add_file_to_emitter(util_env, 'PROGEMITTER', file) |
| 287 |
add_file_to_emitter(plugin_env, 'SHLIBEMITTER', file) |
| 288 |
add_file_to_emitter(stub_uenv, 'PROGEMITTER', file) |
| 289 |
add_file_to_emitter(plugin_uenv, 'SHLIBEMITTER', file) |
| 290 |
|
| 291 |
cenv = defenv.Clone() |
| 292 |
cross_env(cenv) |
| 293 |
conf = cenv.Configure(custom_tests = { 'CheckRequirement' : check_requirement }) |
| 294 |
|
| 295 |
memcpy_test = """ |
| 296 |
struct s { char c[128]; } t = { "test" }; // gcc 3 |
| 297 |
char a[] = {'/', 'F', 'I' ,'L', 'L', 'S', 'C', 'R', 'E', 'E', 'N', 0}; // gcc 4 |
| 298 |
int i; |
| 299 |
for (i = 0; i < 100; i++) i += a[i % sizeof(a)] ^ t.c[i]; // avoid a and t being optimized out |
| 300 |
return i; |
| 301 |
""" |
| 302 |
|
| 303 |
memset_test = """ |
| 304 |
char c[128] = "test"; |
| 305 |
c[0] = '6'; // avoid c being optimized out |
| 306 |
return c[1]; // avoid c being optimized out |
| 307 |
""" |
| 308 |
|
| 309 |
if conf.CheckRequirement('memcpy', memcpy_test): |
| 310 |
add_file('memcpy.c') |
| 311 |
|
| 312 |
if conf.CheckRequirement('memset', memset_test): |
| 313 |
add_file('memset.c') |
| 314 |
|
| 315 |
conf.Finish() |
| 316 |
|
| 317 |
# |
| 318 |
# Some platforms, like FreeBSD, require -pthread flag to be passed |
| 319 |
# instead of -lpthread. |
| 320 |
# |
| 321 |
|
| 322 |
conf = FlagsConfigure(makensis_env) |
| 323 |
conf.CheckLinkFlag('-pthread', codeprepend = code_failonmswin) |
| 324 |
conf.Finish() |
| 325 |
|
| 326 |
# |
| 327 |
# GCC doesn't define __BIG_ENDIAN__ or __LITTLE_ENDIAN__, so manually check |
| 328 |
# for the endianness and define __BIG_ENDIAN__ if needed. |
| 329 |
# |
| 330 |
|
| 331 |
def check_big_endian(ctx): |
| 332 |
ctx.Message('Checking for __BIG_ENDIAN__... ') |
| 333 |
|
| 334 |
test = """ |
| 335 |
int main() { |
| 336 |
#ifdef __BIG_ENDIAN__ |
| 337 |
// already defined, no need to define again |
| 338 |
return 0; |
| 339 |
#else |
| 340 |
int i = 1; |
| 341 |
char *c = (char *) &i; |
| 342 |
return c[0] != 1; |
| 343 |
#endif |
| 344 |
} |
| 345 |
""" |
| 346 |
|
| 347 |
result = not ctx.TryRun(test, '.c')[0] |
| 348 |
ctx.Result(result) |
| 349 |
return result |
| 350 |
|
| 351 |
conf = defenv.Configure(custom_tests = { 'CheckBigEndian' : check_big_endian }) |
| 352 |
if conf.CheckBigEndian(): |
| 353 |
makensis_env.Append(CPPDEFINES = ['__BIG_ENDIAN__']) |
| 354 |
test_env.Append(CPPDEFINES = ['__BIG_ENDIAN__']) |
| 355 |
conf.Finish() |
| 356 |
|
| 357 |
if makensis_env['PLATFORM'] == 'hpux': |
| 358 |
makensis_env.Append(CPPDEFINES = ['NSIS_HPUX_ALLOW_UNALIGNED_DATA_ACCESS']) |
| 359 |
makensis_conf = makensis_env.Configure() |
| 360 |
makensis_conf.CheckLib("unalign") |
| 361 |
makensis_conf.CheckLib("hppa") |
| 362 |
makensis_conf.Finish() |
| 363 |
|
| 364 |
### print version info |
| 365 |
stub_env.Execute('$CC --version') |
| 366 |
makensis_env.Execute('$CXX --version') |
| 367 |
|
| 368 |
### return |
| 369 |
|
| 370 |
Return('stub_env makensis_env plugin_env util_env cp_util_env test_env stub_uenv plugin_uenv') |