@@ -121,47 +121,46 @@ def defines(self):
121
121
ret = [self .ndebug , self .gcc_cxx11_abi ] + conf_flags + self .extra_defines
122
122
return self ._filter_list_empty_fields (ret )
123
123
124
+ def _exe_env_var_to_unix_path (self , env_var , default = None , extra_options = []):
125
+ """
126
+ Convenient method to convert env vars like CC, CXX or LD to values compatible with autotools.
127
+ If env var doesn't exist, returns default.
128
+ """
129
+ exe = get_env (env_var )
130
+ if exe :
131
+ if os .path .exists (exe ):
132
+ exe = unix_path (self ._conanfile , exe )
133
+ else :
134
+ exe = default
135
+ if exe :
136
+ for option in extra_options :
137
+ if option not in exe :
138
+ exe = f"{ exe } { option } "
139
+ return exe
140
+
124
141
def environment (self ):
125
142
env = Environment ()
126
143
127
- # Specific compiler & linker handling on Windows
144
+ # On Windows or if compiler is msvc, ensure to properly set CC, CXX and LD:
145
+ # - convert values from profile (if set) to compatible values
146
+ # - otherwise set to a good default if compiler is not a first class citizen in autotools
128
147
if hasattr (self ._conanfile , "settings_build" ):
129
148
os_build = self ._conanfile .settings_build .get_safe ("os" )
130
149
else :
131
150
os_build = self ._conanfile .settings .get_safe ("os" )
132
- if is_msvc (self ._conanfile ):
133
- cc = get_env ("CC" )
134
- if cc and os .path .exists (cc ):
135
- cc = unix_path (self ._conanfile , cc )
136
- else :
137
- cc = "cl"
138
- env .define ("CC" , f"{ cc } -nologo" )
139
-
140
- cxx = get_env ("CXX" )
141
- if cxx and os .path .exists (cxx ):
142
- cxx = unix_path (self ._conanfile , cxx )
143
- else :
144
- cxx = "cl"
145
- env .define ("CXX" , f"{ cxx } -nologo" )
146
-
147
- ld = get_env ("LD" )
148
- if ld and os .path .exists (ld ):
149
- ld = unix_path (self ._conanfile , ld )
150
- else :
151
- ld = "link"
152
- env .define ("LD" , f"{ ld } -nologo" )
153
- elif os_build == "Windows" :
154
- cc = get_env ("CC" )
155
- if cc and os .path .exists (cc ):
156
- env .define ("CC" , unix_path (self ._conanfile , cc ))
157
-
158
- cxx = get_env ("CXX" )
159
- if cxx and os .path .exists (cxx ):
160
- env .define ("CXX" , unix_path (self ._conanfile , cxx ))
161
-
162
- ld = get_env ("LD" )
163
- if ld and os .path .exists (ld ):
164
- env .define ("LD" , unix_path (self ._conanfile , ld ))
151
+ if is_msvc (self ._conanfile ) or os_build == "Windows" :
152
+ default_compiler = "cl" if is_msvc (self ._conanfile ) else None
153
+ default_linker = "link" if is_msvc (self ._conanfile ) else None
154
+ extra_options = ["-nologo" ] if is_msvc (self ._conanfile ) else []
155
+ cc = self ._exe_env_var_to_unix_path ("CC" , default_compiler , extra_options )
156
+ if cc :
157
+ env .define ("CC" , cc )
158
+ cxx = self ._exe_env_var_to_unix_path ("CXX" , default_compiler , extra_options )
159
+ if cxx :
160
+ env .define ("CXX" , cxx )
161
+ ld = self ._exe_env_var_to_unix_path ("LD" , default_linker , extra_options )
162
+ if ld :
163
+ env .define ("LD" , ld )
165
164
166
165
env .append ("CPPFLAGS" , ["-D{}" .format (d ) for d in self .defines ])
167
166
env .append ("CXXFLAGS" , self .cxxflags )
0 commit comments