José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 1 | """SCons.Tool.gcc |
| 2 | |
| 3 | Tool-specific initialization for MinGW (http://www.mingw.org/) |
| 4 | |
| 5 | There normally shouldn't be any need to import this module directly. |
| 6 | It will usually be imported through the generic SCons.Tool.Tool() |
| 7 | selection method. |
| 8 | |
| 9 | See also http://www.scons.org/wiki/CrossCompilingMingw |
| 10 | """ |
| 11 | |
| 12 | # |
| 13 | # Copyright (c) 2001, 2002, 2003, 2004 The SCons Foundation |
| 14 | # |
| 15 | # Permission is hereby granted, free of charge, to any person obtaining |
| 16 | # a copy of this software and associated documentation files (the |
| 17 | # "Software"), to deal in the Software without restriction, including |
| 18 | # without limitation the rights to use, copy, modify, merge, publish, |
| 19 | # distribute, sublicense, and/or sell copies of the Software, and to |
| 20 | # permit persons to whom the Software is furnished to do so, subject to |
| 21 | # the following conditions: |
| 22 | # |
| 23 | # The above copyright notice and this permission notice shall be included |
| 24 | # in all copies or substantial portions of the Software. |
| 25 | # |
| 26 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY |
| 27 | # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 28 | # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 29 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 30 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 31 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 32 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 33 | # |
| 34 | |
| 35 | import os |
| 36 | import os.path |
| 37 | import string |
| 38 | |
| 39 | import SCons.Action |
| 40 | import SCons.Builder |
| 41 | import SCons.Tool |
| 42 | import SCons.Util |
| 43 | |
José Fonseca | b6956ae | 2014-06-06 13:13:05 +0100 | [diff] [blame] | 44 | # These are the mingw toolchain prefixes we search for: |
| 45 | # (We only search for the mingw-w64 toolchain, and not the mingw.org one.) |
José Fonseca | f816937 | 2010-05-04 21:51:38 +0100 | [diff] [blame] | 46 | prefixes32 = SCons.Util.Split(""" |
Vinson Lee | a6e642b | 2010-09-27 13:13:25 -0700 | [diff] [blame] | 47 | i686-w64-mingw32- |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 48 | """) |
José Fonseca | f816937 | 2010-05-04 21:51:38 +0100 | [diff] [blame] | 49 | prefixes64 = SCons.Util.Split(""" |
José Fonseca | 191428c | 2011-08-29 13:21:24 +0100 | [diff] [blame] | 50 | x86_64-w64-mingw32- |
José Fonseca | f816937 | 2010-05-04 21:51:38 +0100 | [diff] [blame] | 51 | """) |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 52 | |
| 53 | def find(env): |
José Fonseca | f816937 | 2010-05-04 21:51:38 +0100 | [diff] [blame] | 54 | if env['machine'] == 'x86_64': |
| 55 | prefixes = prefixes64 |
| 56 | else: |
| 57 | prefixes = prefixes32 |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 58 | for prefix in prefixes: |
| 59 | # First search in the SCons path and then the OS path: |
| 60 | if env.WhereIs(prefix + 'gcc') or SCons.Util.WhereIs(prefix + 'gcc'): |
| 61 | return prefix |
| 62 | |
| 63 | return '' |
| 64 | |
| 65 | def shlib_generator(target, source, env, for_signature): |
| 66 | cmd = SCons.Util.CLVar(['$SHLINK', '$SHLINKFLAGS']) |
| 67 | |
| 68 | dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX') |
| 69 | if dll: cmd.extend(['-o', dll]) |
| 70 | |
| 71 | cmd.extend(['$SOURCES', '$_LIBDIRFLAGS', '$_LIBFLAGS']) |
| 72 | |
| 73 | implib = env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX') |
| 74 | if implib: cmd.append('-Wl,--out-implib,'+implib.get_string(for_signature)) |
| 75 | |
| 76 | def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX') |
| 77 | if def_target: cmd.append('-Wl,--output-def,'+def_target.get_string(for_signature)) |
| 78 | |
| 79 | return [cmd] |
| 80 | |
| 81 | def shlib_emitter(target, source, env): |
| 82 | dll = env.FindIxes(target, 'SHLIBPREFIX', 'SHLIBSUFFIX') |
| 83 | no_import_lib = env.get('no_import_lib', 0) |
| 84 | |
| 85 | if not dll: |
Eric Engestrom | bb66af9 | 2017-09-19 14:09:01 +0100 | [diff] [blame] | 86 | raise SCons.Errors.UserError("A shared library should have exactly one target with the suffix: %s" % env.subst("$SHLIBSUFFIX")) |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 87 | |
| 88 | if not no_import_lib and \ |
| 89 | not env.FindIxes(target, 'LIBPREFIX', 'LIBSUFFIX'): |
| 90 | |
| 91 | # Append an import library to the list of targets. |
| 92 | target.append(env.ReplaceIxes(dll, |
| 93 | 'SHLIBPREFIX', 'SHLIBSUFFIX', |
| 94 | 'LIBPREFIX', 'LIBSUFFIX')) |
| 95 | |
| 96 | # Append a def file target if there isn't already a def file target |
| 97 | # or a def file source. There is no option to disable def file |
| 98 | # target emitting, because I can't figure out why someone would ever |
| 99 | # want to turn it off. |
| 100 | def_source = env.FindIxes(source, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX') |
| 101 | def_target = env.FindIxes(target, 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX') |
| 102 | if not def_source and not def_target: |
| 103 | target.append(env.ReplaceIxes(dll, |
| 104 | 'SHLIBPREFIX', 'SHLIBSUFFIX', |
| 105 | 'WIN32DEFPREFIX', 'WIN32DEFSUFFIX')) |
| 106 | |
| 107 | return (target, source) |
| 108 | |
| 109 | |
José Fonseca | 3cf92e9 | 2009-06-19 15:39:59 +0100 | [diff] [blame] | 110 | shlib_action = SCons.Action.Action(shlib_generator, '$SHLINKCOMSTR', generator=1) |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 111 | |
| 112 | res_action = SCons.Action.Action('$RCCOM', '$RCCOMSTR') |
| 113 | |
| 114 | res_builder = SCons.Builder.Builder(action=res_action, suffix='.o', |
| 115 | source_scanner=SCons.Tool.SourceFileScanner) |
| 116 | SCons.Tool.SourceFileScanner.add_scanner('.rc', SCons.Defaults.CScan) |
| 117 | |
Brian Paul | cdb7396 | 2011-08-25 16:50:56 -0600 | [diff] [blame] | 118 | |
| 119 | |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 120 | def generate(env): |
| 121 | mingw_prefix = find(env) |
| 122 | |
| 123 | if mingw_prefix: |
| 124 | dir = os.path.dirname(env.WhereIs(mingw_prefix + 'gcc') or SCons.Util.WhereIs(mingw_prefix + 'gcc')) |
| 125 | |
| 126 | # The mingw bin directory must be added to the path: |
| 127 | path = env['ENV'].get('PATH', []) |
| 128 | if not path: |
| 129 | path = [] |
| 130 | if SCons.Util.is_String(path): |
Michel Zou | 3f92d17 | 2019-09-28 08:53:38 +0200 | [diff] [blame] | 131 | path = str.split(path, os.pathsep) |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 132 | |
Michel Zou | 3f92d17 | 2019-09-28 08:53:38 +0200 | [diff] [blame] | 133 | env['ENV']['PATH'] = str.join(os.pathsep, [dir] + path) |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 134 | |
| 135 | # Most of mingw is the same as gcc and friends... |
| 136 | gnu_tools = ['gcc', 'g++', 'gnulink', 'ar', 'gas'] |
| 137 | for tool in gnu_tools: |
| 138 | SCons.Tool.Tool(tool)(env) |
| 139 | |
| 140 | #... but a few things differ: |
| 141 | env['CC'] = mingw_prefix + 'gcc' |
| 142 | env['SHCCFLAGS'] = SCons.Util.CLVar('$CCFLAGS') |
| 143 | env['CXX'] = mingw_prefix + 'g++' |
| 144 | env['SHCXXFLAGS'] = SCons.Util.CLVar('$CXXFLAGS') |
| 145 | env['SHLINKFLAGS'] = SCons.Util.CLVar('$LINKFLAGS -shared') |
| 146 | env['SHLINKCOM'] = shlib_action |
| 147 | env.Append(SHLIBEMITTER = [shlib_emitter]) |
| 148 | env['LINK'] = mingw_prefix + 'g++' |
| 149 | env['AR'] = mingw_prefix + 'ar' |
| 150 | env['RANLIB'] = mingw_prefix + 'ranlib' |
| 151 | env['LINK'] = mingw_prefix + 'g++' |
| 152 | env['AS'] = mingw_prefix + 'as' |
| 153 | env['WIN32DEFPREFIX'] = '' |
| 154 | env['WIN32DEFSUFFIX'] = '.def' |
| 155 | env['SHOBJSUFFIX'] = '.o' |
| 156 | env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1 |
| 157 | |
| 158 | env['RC'] = mingw_prefix + 'windres' |
| 159 | env['RCFLAGS'] = SCons.Util.CLVar('') |
| 160 | env['RCCOM'] = '$RC $_CPPDEFFLAGS $_CPPINCFLAGS ${INCPREFIX}${SOURCE.dir} $RCFLAGS -i $SOURCE -o $TARGET' |
| 161 | env['BUILDERS']['RES'] = res_builder |
| 162 | |
| 163 | # Some setting from the platform also have to be overridden: |
| 164 | env['OBJPREFIX'] = '' |
| 165 | env['OBJSUFFIX'] = '.o' |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 166 | env['SHOBJPREFIX'] = '$OBJPREFIX' |
| 167 | env['SHOBJSUFFIX'] = '$OBJSUFFIX' |
| 168 | env['PROGPREFIX'] = '' |
| 169 | env['PROGSUFFIX'] = '.exe' |
José Fonseca | 83155aa | 2009-01-14 11:36:02 +0000 | [diff] [blame] | 170 | env['LIBPREFIX'] = 'lib' |
| 171 | env['LIBSUFFIX'] = '.a' |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 172 | env['SHLIBPREFIX'] = '' |
| 173 | env['SHLIBSUFFIX'] = '.dll' |
José Fonseca | 83155aa | 2009-01-14 11:36:02 +0000 | [diff] [blame] | 174 | env['LIBPREFIXES'] = [ 'lib', '' ] |
| 175 | env['LIBSUFFIXES'] = [ '.a', '.lib' ] |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 176 | |
José Fonseca | 6cf59e1 | 2008-11-18 19:13:32 +0900 | [diff] [blame] | 177 | def exists(env): |
| 178 | return find(env) |