Debian Codegraph @

tools/crossmingw.py

raw
1
2 import os
3 import os.path
4 import string
5
6 import SCons.Action
7 import SCons.Builder
8 import SCons.Defaults
9 import SCons.Tool
10 import SCons.Util
11
12 # This is what we search for to find mingw:
13 prefixes = SCons.Util.Split("""
14 mingw32-
15 i386-mingw32msvc-
16 i486-mingw32msvc-
17 i586-mingw32msvc-
18 i686-mingw32msvc-
19 i686-w64-mingw32-
20 """)
21
22 def find(env):
23 for prefix in prefixes:
24 # First search in the SCons path and then the OS path:
25 if env.WhereIs(prefix + 'gcc') or SCons.Util.WhereIs(prefix + 'gcc'):
26 return prefix
27
28 return ''
29
30 def shlib_generator(target, source, env, for_signature):
31 cmd = SCons.Util.CLVar(['$SHLINK', '$SHLINKFLAGS'])
32
33 dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
34 if dll: cmd.extend(['-o', dll])
35
36 cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS'])
37
38 implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX')
39 if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature))
40
41 def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
42 if def_target: cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature))
43
44 return [cmd]
45
46 def shlib_emitter(target, source, env):
47 dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX')
48 no_import_lib = env.get('no_import_lib', 0)
49
50 if not dll:
51 raise SCons.Errors.UserError, "A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")
52
53 if not no_import_lib and \
54 not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'):
55
56 # Append an import library to the list of targets.
57 target.append(env.ReplaceIxes(dll,
58 'SHLIBPREFIX', 'SHLIBSUFFIX',
59 'LIBPREFIX', 'LIBSUFFIX'))
60
61 # Append a def file target if there isn't already a def file target
62 # or a def file source. There is no option to disable def file
63 # target emitting, because I can't figure out why someone would ever
64 # want to turn it off.
65 def_source = env.FindIxes(source, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
66 def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')
67 if not def_source and not def_target:
68 target.append(env.ReplaceIxes(dll,
69 'SHLIBPREFIX', 'SHLIBSUFFIX',
70 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX'))
71
72 return (target, source)
73
74
75 shlib_action = SCons.Action.Action(shlib_generator, generator=1)
76
77 res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR')
78
79 res_builder = SCons.Builder.Builder(action=res_action, suffix='.o',
80 source_scanner=SCons.Tool.SourceFileScanner)
81 SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan)
82
83 def generate(env):
84 mingw_prefix = find(env)
85
86 if mingw_prefix:
87 dir = os.path.dirname(env.WhereIs(mingw_prefix + 'gcc') or SCons.Util.WhereIs(mingw_prefix + 'gcc'))
88
89 # The mingw bin directory must be added to the path:
90 path = env['ENV'].get('PATH', [])
91 if not path:
92 path = []
93 if SCons.Util.is_String(path):
94 path = path.split(os.pathsep)
95
96 env['ENV']['PATH'] = os.pathsep.join([dir] + path)
97
98 # Most of mingw is the same as gcc and friends...
99 gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas']
100 for tool in gnu_tools:
101 SCons.Tool.Tool(tool)(env)
102
103 #... but a few things differ:
104 env['CC'] = mingw_prefix + 'gcc'
105 env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS')
106 env['CXX'] = mingw_prefix + 'g++'
107 env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS')
108 env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared')
109 env['SHLINKCOM'] = shlib_action
110 env.Append(SHLIBEMITTER = [shlib_emitter])
111 # This line isn't required and breaks C++ linking
112 #env['LINK'] = mingw_prefix + 'g++'
113 env['AS'] = mingw_prefix + 'as'
114 env['AR'] = mingw_prefix + 'ar'
115 env['RANLIB'] = mingw_prefix + 'ranlib'
116 env['WIN32DEFPREFIX'] = ''
117 env['WIN32DEFSUFFIX'] = '.def'
118 env['SHOBJSUFFIX'] = '.o'
119 env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1
120
121 env['RC'] = mingw_prefix + 'windres'
122 env['RCFLAGS'] = SCons.Util.CLVar('')
123 env['RCINCFLAGS'] = '$( ${_concat(RCINCPREFIX, CPPPATH, RCINCSUFFIX, __env__, RDirs, TARGET)} $)'
124 env['RCINCPREFIX'] = '--include-dir '
125 env['RCINCSUFFIX'] = ''
126 env['RCCOM'] = '$RC $RCINCFLAGS $RCINCPREFIX $SOURCE.dir $RCFLAGS -i $SOURCE -o $TARGET'
127 env['BUILDERS']['RES'] = res_builder
128
129 # Some setting from the platform also have to be overridden:
130 env['OBJPREFIX'] = ''
131 env['OBJSUFFIX'] = '.o'
132 env['LIBPREFIX'] = 'lib'
133 env['LIBSUFFIX'] = '.a'
134 env['SHOBJPREFIX'] = '$OBJPREFIX'
135 env['SHOBJSUFFIX'] = '$OBJSUFFIX'
136 env['PROGPREFIX'] = ''
137 env['PROGSUFFIX'] = '.exe'
138 env['LIBPREFIX'] = ''
139 env['LIBSUFFIX'] = '.lib'
140 env['SHLIBPREFIX'] = ''
141 env['SHLIBSUFFIX'] = '.dll'
142 env['LIBPREFIXES'] = [ '$LIBPREFIX' ]
143 env['LIBSUFFIXES'] = [ '$LIBSUFFIX' ]
144
145 def exists(env):
146 return find(env)