@@ -255,7 +255,8 @@ def sanity_check_impl(self, work_dir, environment, sname, code):
255
255
# a ton of compiler flags to differentiate between
256
256
# arm and x86_64. So just compile.
257
257
mode = 'compile'
258
- extra_flags = self ._get_basic_compiler_args (environment , mode )
258
+ cargs , largs = self ._get_basic_compiler_args (environment , mode )
259
+ extra_flags = cargs + self .linker_to_compiler_args (largs )
259
260
260
261
# Is a valid executable output for all toolchains and platforms
261
262
binname += '.exe'
@@ -264,7 +265,9 @@ def sanity_check_impl(self, work_dir, environment, sname, code):
264
265
with open (source_name , 'w' ) as ofile :
265
266
ofile .write (code )
266
267
# Compile sanity check
267
- cmdlist = self .exelist + extra_flags + [source_name ] + self .get_output_args (binary_name )
268
+ # NOTE: extra_flags must be added at the end. On MSVC, it might contain a '/link' argument
269
+ # after which all further arguments will be passed directly to the linker
270
+ cmdlist = self .exelist + [source_name ] + self .get_output_args (binary_name ) + extra_flags
268
271
pc , stdo , stde = mesonlib .Popen_safe (cmdlist , cwd = work_dir )
269
272
mlog .debug ('Sanity check compiler command line:' , ' ' .join (cmdlist ))
270
273
mlog .debug ('Sanity check compile stdout:' )
@@ -329,10 +332,10 @@ def has_header_symbol(self, hname, symbol, prefix, env, *, extra_args=None, depe
329
332
dependencies = dependencies )
330
333
331
334
def _get_basic_compiler_args (self , env , mode ):
332
- args = []
335
+ cargs , largs = [], []
333
336
# Select a CRT if needed since we're linking
334
337
if mode == 'link' :
335
- args += self .get_linker_debug_crt_args ()
338
+ cargs += self .get_linker_debug_crt_args ()
336
339
337
340
# Add CFLAGS/CXXFLAGS/OBJCFLAGS/OBJCXXFLAGS and CPPFLAGS from the env
338
341
sys_args = env .coredata .get_external_args (self .for_machine , self .language )
@@ -341,17 +344,17 @@ def _get_basic_compiler_args(self, env, mode):
341
344
# also used during linking. These flags can break
342
345
# argument checks. Thanks, Autotools.
343
346
cleaned_sys_args = self .remove_linkerlike_args (sys_args )
344
- args += cleaned_sys_args
347
+ cargs += cleaned_sys_args
345
348
346
349
if mode == 'link' :
347
350
# Add LDFLAGS from the env
348
351
sys_ld_args = env .coredata .get_external_link_args (self .for_machine , self .language )
349
352
# CFLAGS and CXXFLAGS go to both linking and compiling, but we want them
350
353
# to only appear on the command line once. Remove dupes.
351
- args += [x for x in sys_ld_args if x not in sys_args ]
354
+ largs += [x for x in sys_ld_args if x not in sys_args ]
352
355
353
- args += self .get_compiler_args_for_mode (mode )
354
- return args
356
+ cargs += self .get_compiler_args_for_mode (mode )
357
+ return cargs , largs
355
358
356
359
def _get_compiler_check_args (self , env , extra_args , dependencies , mode = 'compile' ):
357
360
if extra_args is None :
@@ -365,19 +368,27 @@ def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'
365
368
elif not isinstance (dependencies , list ):
366
369
dependencies = [dependencies ]
367
370
# Collect compiler arguments
368
- args = compilers .CompilerArgs (self )
371
+ cargs = compilers .CompilerArgs (self )
372
+ largs = []
369
373
for d in dependencies :
370
374
# Add compile flags needed by dependencies
371
- args += d .get_compile_args ()
375
+ cargs += d .get_compile_args ()
372
376
if mode == 'link' :
373
377
# Add link flags needed to find dependencies
374
- args += d .get_link_args ()
378
+ largs += d .get_link_args ()
379
+
380
+ ca , la = self ._get_basic_compiler_args (env , mode )
381
+ cargs += ca
382
+ largs += la
375
383
376
- args += self ._get_basic_compiler_args ( env , mode )
384
+ cargs += self .get_compiler_check_args ( )
377
385
378
- args += self .get_compiler_check_args ()
379
- # extra_args must override all other arguments, so we add them last
380
- args += extra_args
386
+ # on MSVC compiler and linker flags must be separated by the "/link" argument
387
+ # at this point, the '/link' argument may already be part of extra_args, otherwise, it is added here
388
+ if self .linker_to_compiler_args ([]) == ['/link' ] and largs != [] and not ('/link' in extra_args ):
389
+ extra_args += ['/link' ]
390
+
391
+ args = cargs + extra_args + largs
381
392
return args
382
393
383
394
def compiles (self , code , env , * , extra_args = None , dependencies = None , mode = 'compile' , disable_cache = False ):
@@ -964,10 +975,12 @@ def find_library_real(self, libname, env, extra_dirs, code, libtype: LibType):
964
975
# search for .a. This is only allowed if libtype is LibType.PREFER_SHARED
965
976
if ((not extra_dirs and libtype is LibType .PREFER_SHARED ) or
966
977
libname in self .internal_libs ):
967
- args = ['-l' + libname ]
968
- largs = self .linker_to_compiler_args (self .get_allow_undefined_link_args ())
969
- if self .links (code , env , extra_args = (args + largs ), disable_cache = True )[0 ]:
970
- return args
978
+ cargs = ['-l' + libname ]
979
+ largs = self .get_allow_undefined_link_args ()
980
+ extra_args = cargs + self .linker_to_compiler_args (largs )
981
+
982
+ if self .links (code , env , extra_args = extra_args , disable_cache = True )[0 ]:
983
+ return cargs
971
984
# Don't do a manual search for internal libs
972
985
if libname in self .internal_libs :
973
986
return None
0 commit comments