forked from mesonbuild/meson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
3101 lines (2653 loc) · 131 KB
/
build.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2012-2017 The Meson development team
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
from collections import defaultdict, OrderedDict
from dataclasses import dataclass, field, InitVar
from functools import lru_cache
import abc
import hashlib
import itertools, pathlib
import os
import pickle
import re
import textwrap
import typing as T
from . import coredata
from . import dependencies
from . import mlog
from . import programs
from .mesonlib import (
HoldableObject, SecondLevelHolder,
File, MesonException, MachineChoice, PerMachine, OrderedSet, listify,
extract_as_list, typeslistify, stringlistify, classify_unity_sources,
get_filenames_templates_dict, substitute_values, has_path_sep,
OptionKey, PerMachineDefaultable,
MesonBugException, EnvironmentVariables, pickle_load,
)
from .compilers import (
is_header, is_object, is_source, clink_langs, sort_clink, all_languages,
is_known_suffix, detect_static_linker
)
from .interpreterbase import FeatureNew, FeatureDeprecated
if T.TYPE_CHECKING:
from typing_extensions import Literal, TypedDict
from . import environment
from ._typing import ImmutableListProtocol
from .backend.backends import Backend
from .compilers import Compiler
from .interpreter.interpreter import SourceOutputs, Interpreter
from .interpreter.interpreterobjects import Test
from .interpreterbase import SubProject
from .linkers.linkers import StaticLinker
from .mesonlib import ExecutableSerialisation, FileMode, FileOrString
from .modules import ModuleState
from .mparser import BaseNode
from .wrap import WrapMode
GeneratedTypes = T.Union['CustomTarget', 'CustomTargetIndex', 'GeneratedList']
LibTypes = T.Union['SharedLibrary', 'StaticLibrary', 'CustomTarget', 'CustomTargetIndex']
BuildTargetTypes = T.Union['BuildTarget', 'CustomTarget', 'CustomTargetIndex']
ObjectTypes = T.Union[str, 'File', 'ExtractedObjects', 'GeneratedTypes']
class DFeatures(TypedDict):
unittest: bool
debug: T.List[T.Union[str, int]]
import_dirs: T.List[IncludeDirs]
versions: T.List[T.Union[str, int]]
pch_kwargs = {'c_pch', 'cpp_pch'}
lang_arg_kwargs = {f'{lang}_args' for lang in all_languages}
lang_arg_kwargs |= {
'd_import_dirs',
'd_unittest',
'd_module_versions',
'd_debug',
}
vala_kwargs = {'vala_header', 'vala_gir', 'vala_vapi'}
rust_kwargs = {'rust_crate_type', 'rust_dependency_map'}
cs_kwargs = {'resources', 'cs_args'}
buildtarget_kwargs = {
'build_by_default',
'build_rpath',
'dependencies',
'extra_files',
'gui_app',
'link_with',
'link_whole',
'link_args',
'link_depends',
'implicit_include_directories',
'include_directories',
'install',
'install_rpath',
'install_dir',
'install_mode',
'install_tag',
'name_prefix',
'name_suffix',
'native',
'objects',
'override_options',
'sources',
'gnu_symbol_visibility',
'link_language',
'win_subsystem',
}
known_build_target_kwargs = (
buildtarget_kwargs |
lang_arg_kwargs |
pch_kwargs |
vala_kwargs |
rust_kwargs |
cs_kwargs)
known_exe_kwargs = known_build_target_kwargs | {'implib', 'export_dynamic', 'pie', 'vs_module_defs'}
known_shlib_kwargs = known_build_target_kwargs | {'version', 'soversion', 'vs_module_defs', 'darwin_versions', 'rust_abi'}
known_shmod_kwargs = known_build_target_kwargs | {'vs_module_defs', 'rust_abi'}
known_stlib_kwargs = known_build_target_kwargs | {'pic', 'prelink', 'rust_abi'}
known_jar_kwargs = known_exe_kwargs | {'main_class', 'java_resources'}
def _process_install_tag(install_tag: T.Optional[T.List[T.Optional[str]]],
num_outputs: int) -> T.List[T.Optional[str]]:
_install_tag: T.List[T.Optional[str]]
if not install_tag:
_install_tag = [None] * num_outputs
elif len(install_tag) == 1:
_install_tag = install_tag * num_outputs
else:
_install_tag = install_tag
return _install_tag
@lru_cache(maxsize=None)
def get_target_macos_dylib_install_name(ld) -> str:
name = ['@rpath/', ld.prefix, ld.name]
if ld.soversion is not None:
name.append('.' + ld.soversion)
name.append('.dylib')
return ''.join(name)
class InvalidArguments(MesonException):
pass
@dataclass(eq=False)
class DependencyOverride(HoldableObject):
dep: dependencies.Dependency
node: 'BaseNode'
explicit: bool = True
@dataclass(eq=False)
class Headers(HoldableObject):
sources: T.List[File]
install_subdir: T.Optional[str]
custom_install_dir: T.Optional[str]
custom_install_mode: 'FileMode'
subproject: str
follow_symlinks: T.Optional[bool] = None
# TODO: we really don't need any of these methods, but they're preserved to
# keep APIs relying on them working.
def set_install_subdir(self, subdir: str) -> None:
self.install_subdir = subdir
def get_install_subdir(self) -> T.Optional[str]:
return self.install_subdir
def get_sources(self) -> T.List[File]:
return self.sources
def get_custom_install_dir(self) -> T.Optional[str]:
return self.custom_install_dir
def get_custom_install_mode(self) -> 'FileMode':
return self.custom_install_mode
@dataclass(eq=False)
class Man(HoldableObject):
sources: T.List[File]
custom_install_dir: T.Optional[str]
custom_install_mode: 'FileMode'
subproject: str
locale: T.Optional[str]
def get_custom_install_dir(self) -> T.Optional[str]:
return self.custom_install_dir
def get_custom_install_mode(self) -> 'FileMode':
return self.custom_install_mode
def get_sources(self) -> T.List['File']:
return self.sources
@dataclass(eq=False)
class EmptyDir(HoldableObject):
path: str
install_mode: 'FileMode'
subproject: str
install_tag: T.Optional[str] = None
@dataclass(eq=False)
class InstallDir(HoldableObject):
source_subdir: str
installable_subdir: str
install_dir: str
install_dir_name: str
install_mode: 'FileMode'
exclude: T.Tuple[T.Set[str], T.Set[str]]
strip_directory: bool
subproject: str
from_source_dir: bool = True
install_tag: T.Optional[str] = None
follow_symlinks: T.Optional[bool] = None
@dataclass(eq=False)
class DepManifest:
version: str
license: T.List[str]
license_files: T.List[T.Tuple[str, File]]
subproject: str
def to_json(self) -> T.Dict[str, T.Union[str, T.List[str]]]:
return {
'version': self.version,
'license': self.license,
'license_files': [l[1].relative_name() for l in self.license_files],
}
# literally everything isn't dataclass stuff
class Build:
"""A class that holds the status of one build including
all dependencies and so on.
"""
def __init__(self, environment: environment.Environment):
self.version = coredata.version
self.project_name = 'name of master project'
self.project_version = None
self.environment = environment
self.projects = {}
self.targets: 'T.OrderedDict[str, T.Union[CustomTarget, BuildTarget]]' = OrderedDict()
self.targetnames: T.Set[T.Tuple[str, str]] = set() # Set of executable names and their subdir
self.global_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachine({}, {})
self.global_link_args: PerMachine[T.Dict[str, T.List[str]]] = PerMachine({}, {})
self.projects_args: PerMachine[T.Dict[str, T.Dict[str, T.List[str]]]] = PerMachine({}, {})
self.projects_link_args: PerMachine[T.Dict[str, T.Dict[str, T.List[str]]]] = PerMachine({}, {})
self.tests: T.List['Test'] = []
self.benchmarks: T.List['Test'] = []
self.headers: T.List[Headers] = []
self.man: T.List[Man] = []
self.emptydir: T.List[EmptyDir] = []
self.data: T.List[Data] = []
self.symlinks: T.List[SymlinkData] = []
self.static_linker: PerMachine[StaticLinker] = PerMachine(None, None)
self.subprojects = {}
self.subproject_dir = ''
self.install_scripts: T.List['ExecutableSerialisation'] = []
self.postconf_scripts: T.List['ExecutableSerialisation'] = []
self.dist_scripts: T.List['ExecutableSerialisation'] = []
self.install_dirs: T.List[InstallDir] = []
self.dep_manifest_name: T.Optional[str] = None
self.dep_manifest: T.Dict[str, DepManifest] = {}
self.stdlibs = PerMachine({}, {})
self.test_setups: T.Dict[str, TestSetup] = {}
self.test_setup_default_name = None
self.find_overrides: T.Dict[str, T.Union['Executable', programs.ExternalProgram, programs.OverrideProgram]] = {}
self.searched_programs: T.Set[str] = set() # The list of all programs that have been searched for.
# If we are doing a cross build we need two caches, if we're doing a
# build == host compilation the both caches should point to the same place.
self.dependency_overrides: PerMachine[T.Dict[T.Tuple, DependencyOverride]] = PerMachineDefaultable.default(
environment.is_cross_build(), {}, {})
self.devenv: T.List[EnvironmentVariables] = []
self.modules: T.List[str] = []
def get_build_targets(self):
build_targets = OrderedDict()
for name, t in self.targets.items():
if isinstance(t, BuildTarget):
build_targets[name] = t
return build_targets
def get_custom_targets(self):
custom_targets = OrderedDict()
for name, t in self.targets.items():
if isinstance(t, CustomTarget):
custom_targets[name] = t
return custom_targets
def copy(self) -> Build:
other = Build(self.environment)
for k, v in self.__dict__.items():
if isinstance(v, (list, dict, set, OrderedDict)):
other.__dict__[k] = v.copy()
else:
other.__dict__[k] = v
return other
def merge(self, other: Build) -> None:
for k, v in other.__dict__.items():
self.__dict__[k] = v
def ensure_static_linker(self, compiler: Compiler) -> None:
if self.static_linker[compiler.for_machine] is None and compiler.needs_static_linker():
self.static_linker[compiler.for_machine] = detect_static_linker(self.environment, compiler)
def get_project(self):
return self.projects['']
def get_subproject_dir(self):
return self.subproject_dir
def get_targets(self) -> 'T.OrderedDict[str, T.Union[CustomTarget, BuildTarget]]':
return self.targets
def get_tests(self) -> T.List['Test']:
return self.tests
def get_benchmarks(self) -> T.List['Test']:
return self.benchmarks
def get_headers(self) -> T.List['Headers']:
return self.headers
def get_man(self) -> T.List['Man']:
return self.man
def get_data(self) -> T.List['Data']:
return self.data
def get_symlinks(self) -> T.List['SymlinkData']:
return self.symlinks
def get_emptydir(self) -> T.List['EmptyDir']:
return self.emptydir
def get_install_subdirs(self) -> T.List['InstallDir']:
return self.install_dirs
def get_global_args(self, compiler: 'Compiler', for_machine: 'MachineChoice') -> T.List[str]:
d = self.global_args[for_machine]
return d.get(compiler.get_language(), [])
def get_project_args(self, compiler: 'Compiler', project: str, for_machine: 'MachineChoice') -> T.List[str]:
d = self.projects_args[for_machine]
args = d.get(project)
if not args:
return []
return args.get(compiler.get_language(), [])
def get_global_link_args(self, compiler: 'Compiler', for_machine: 'MachineChoice') -> T.List[str]:
d = self.global_link_args[for_machine]
return d.get(compiler.get_language(), [])
def get_project_link_args(self, compiler: 'Compiler', project: str, for_machine: 'MachineChoice') -> T.List[str]:
d = self.projects_link_args[for_machine]
link_args = d.get(project)
if not link_args:
return []
return link_args.get(compiler.get_language(), [])
@dataclass(eq=False)
class IncludeDirs(HoldableObject):
"""Internal representation of an include_directories call."""
curdir: str
incdirs: T.List[str]
is_system: bool
# Interpreter has validated that all given directories
# actually exist.
extra_build_dirs: T.List[str] = field(default_factory=list)
def __repr__(self) -> str:
r = '<{} {}/{}>'
return r.format(self.__class__.__name__, self.curdir, self.incdirs)
def get_curdir(self) -> str:
return self.curdir
def get_incdirs(self) -> T.List[str]:
return self.incdirs
def get_extra_build_dirs(self) -> T.List[str]:
return self.extra_build_dirs
def to_string_list(self, sourcedir: str, builddir: str) -> T.List[str]:
"""Convert IncludeDirs object to a list of strings.
:param sourcedir: The absolute source directory
:param builddir: The absolute build directory, option, build dir will not
be added if this is unset
:returns: A list of strings (without compiler argument)
"""
strlist: T.List[str] = []
for idir in self.incdirs:
strlist.append(os.path.join(sourcedir, self.curdir, idir))
strlist.append(os.path.join(builddir, self.curdir, idir))
return strlist
@dataclass(eq=False)
class ExtractedObjects(HoldableObject):
'''
Holds a list of sources for which the objects must be extracted
'''
target: 'BuildTarget'
srclist: T.List[File] = field(default_factory=list)
genlist: T.List['GeneratedTypes'] = field(default_factory=list)
objlist: T.List[T.Union[str, 'File', 'ExtractedObjects']] = field(default_factory=list)
recursive: bool = True
pch: bool = False
def __post_init__(self) -> None:
if self.target.is_unity:
self.check_unity_compatible()
def __repr__(self) -> str:
r = '<{0} {1!r}: {2}>'
return r.format(self.__class__.__name__, self.target.name, self.srclist)
@staticmethod
def get_sources(sources: T.Sequence['FileOrString'], generated_sources: T.Sequence['GeneratedTypes']) -> T.List['FileOrString']:
# Merge sources and generated sources
sources = list(sources)
for gensrc in generated_sources:
for s in gensrc.get_outputs():
# We cannot know the path where this source will be generated,
# but all we need here is the file extension to determine the
# compiler.
sources.append(s)
# Filter out headers and all non-source files
return [s for s in sources if is_source(s)]
def classify_all_sources(self, sources: T.List[FileOrString], generated_sources: T.Sequence['GeneratedTypes']) -> T.Dict['Compiler', T.List['FileOrString']]:
sources_ = self.get_sources(sources, generated_sources)
return classify_unity_sources(self.target.compilers.values(), sources_)
def check_unity_compatible(self) -> None:
# Figure out if the extracted object list is compatible with a Unity
# build. When we're doing a Unified build, we go through the sources,
# and create a single source file from each subset of the sources that
# can be compiled with a specific compiler. Then we create one object
# from each unified source file. So for each compiler we can either
# extra all its sources or none.
cmpsrcs = self.classify_all_sources(self.target.sources, self.target.generated)
extracted_cmpsrcs = self.classify_all_sources(self.srclist, self.genlist)
for comp, srcs in extracted_cmpsrcs.items():
if set(srcs) != set(cmpsrcs[comp]):
raise MesonException('Single object files cannot be extracted '
'in Unity builds. You can only extract all '
'the object files for each compiler at once.')
@dataclass(eq=False, order=False)
class StructuredSources(HoldableObject):
"""A container for sources in languages that use filesystem hierarchy.
Languages like Rust and Cython rely on the layout of files in the filesystem
as part of the compiler implementation. This structure allows us to
represent the required filesystem layout.
"""
sources: T.DefaultDict[str, T.List[T.Union[File, CustomTarget, CustomTargetIndex, GeneratedList]]] = field(
default_factory=lambda: defaultdict(list))
def __add__(self, other: StructuredSources) -> StructuredSources:
sources = self.sources.copy()
for k, v in other.sources.items():
sources[k].extend(v)
return StructuredSources(sources)
def __bool__(self) -> bool:
return bool(self.sources)
def first_file(self) -> T.Union[File, CustomTarget, CustomTargetIndex, GeneratedList]:
"""Get the first source in the root
:return: The first source in the root
"""
return self.sources[''][0]
def as_list(self) -> T.List[T.Union[File, CustomTarget, CustomTargetIndex, GeneratedList]]:
return list(itertools.chain.from_iterable(self.sources.values()))
def needs_copy(self) -> bool:
"""Do we need to create a structure in the build directory.
This allows us to avoid making copies if the structures exists in the
source dir. Which could happen in situations where a generated source
only exists in some configurations
"""
for files in self.sources.values():
for f in files:
if isinstance(f, File):
if f.is_built:
return True
else:
return True
return False
@dataclass(eq=False)
class Target(HoldableObject, metaclass=abc.ABCMeta):
name: str
subdir: str
subproject: 'SubProject'
build_by_default: bool
for_machine: MachineChoice
environment: environment.Environment
install: bool = False
build_always_stale: bool = False
extra_files: T.List[File] = field(default_factory=list)
override_options: InitVar[T.Optional[T.Dict[OptionKey, str]]] = None
@abc.abstractproperty
def typename(self) -> str:
pass
@abc.abstractmethod
def type_suffix(self) -> str:
pass
def __post_init__(self, overrides: T.Optional[T.Dict[OptionKey, str]]) -> None:
if overrides:
ovr = {k.evolve(machine=self.for_machine) if k.lang else k: v
for k, v in overrides.items()}
else:
ovr = {}
self.options = coredata.OptionsView(self.environment.coredata.options, self.subproject, ovr)
# XXX: this should happen in the interpreter
if has_path_sep(self.name):
# Fix failing test 53 when this becomes an error.
mlog.warning(textwrap.dedent(f'''\
Target "{self.name}" has a path separator in its name.
This is not supported, it can cause unexpected failures and will become
a hard error in the future.'''))
# dataclass comparators?
def __lt__(self, other: object) -> bool:
if not isinstance(other, Target):
return NotImplemented
return self.get_id() < other.get_id()
def __le__(self, other: object) -> bool:
if not isinstance(other, Target):
return NotImplemented
return self.get_id() <= other.get_id()
def __gt__(self, other: object) -> bool:
if not isinstance(other, Target):
return NotImplemented
return self.get_id() > other.get_id()
def __ge__(self, other: object) -> bool:
if not isinstance(other, Target):
return NotImplemented
return self.get_id() >= other.get_id()
def get_default_install_dir(self) -> T.Tuple[str, str]:
raise NotImplementedError
def get_custom_install_dir(self) -> T.List[T.Union[str, Literal[False]]]:
raise NotImplementedError
def get_install_dir(self) -> T.Tuple[T.List[T.Union[str, Literal[False]]], str, Literal[False]]:
# Find the installation directory.
default_install_dir, default_install_dir_name = self.get_default_install_dir()
outdirs = self.get_custom_install_dir()
if outdirs and outdirs[0] != default_install_dir and outdirs[0] is not True:
# Either the value is set to a non-default value, or is set to
# False (which means we want this specific output out of many
# outputs to not be installed).
custom_install_dir = True
install_dir_names = [getattr(i, 'optname', None) for i in outdirs]
else:
custom_install_dir = False
# if outdirs is empty we need to set to something, otherwise we set
# only the first value to the default.
if outdirs:
outdirs[0] = default_install_dir
else:
outdirs = [default_install_dir]
install_dir_names = [default_install_dir_name] * len(outdirs)
return outdirs, install_dir_names, custom_install_dir
def get_basename(self) -> str:
return self.name
def get_subdir(self) -> str:
return self.subdir
def get_typename(self) -> str:
return self.typename
@staticmethod
def _get_id_hash(target_id: str) -> str:
# We don't really need cryptographic security here.
# Small-digest hash function with unlikely collision is good enough.
h = hashlib.sha256()
h.update(target_id.encode(encoding='utf-8', errors='replace'))
# This ID should be case-insensitive and should work in Visual Studio,
# e.g. it should not start with leading '-'.
return h.hexdigest()[:7]
@staticmethod
def construct_id_from_path(subdir: str, name: str, type_suffix: str) -> str:
"""Construct target ID from subdir, name and type suffix.
This helper function is made public mostly for tests."""
# This ID must also be a valid file name on all OSs.
# It should also avoid shell metacharacters for obvious
# reasons. '@' is not used as often as '_' in source code names.
# In case of collisions consider using checksums.
# FIXME replace with assert when slash in names is prohibited
name_part = name.replace('/', '@').replace('\\', '@')
assert not has_path_sep(type_suffix)
my_id = name_part + type_suffix
if subdir:
subdir_part = Target._get_id_hash(subdir)
# preserve myid for better debuggability
return subdir_part + '@@' + my_id
return my_id
def get_id(self) -> str:
name = self.name
if getattr(self, 'name_suffix_set', False):
name += '.' + self.suffix
return self.construct_id_from_path(
self.subdir, name, self.type_suffix())
def process_kwargs_base(self, kwargs: T.Dict[str, T.Any]) -> None:
if 'build_by_default' in kwargs:
self.build_by_default = kwargs['build_by_default']
if not isinstance(self.build_by_default, bool):
raise InvalidArguments('build_by_default must be a boolean value.')
if not self.build_by_default and kwargs.get('install', False):
# For backward compatibility, if build_by_default is not explicitly
# set, use the value of 'install' if it's enabled.
self.build_by_default = True
self.set_option_overrides(self.parse_overrides(kwargs))
def set_option_overrides(self, option_overrides: T.Dict[OptionKey, str]) -> None:
self.options.overrides = {}
for k, v in option_overrides.items():
if k.lang:
self.options.overrides[k.evolve(machine=self.for_machine)] = v
else:
self.options.overrides[k] = v
def get_options(self) -> coredata.OptionsView:
return self.options
def get_option(self, key: 'OptionKey') -> T.Union[str, int, bool, 'WrapMode']:
# We don't actually have wrapmode here to do an assert, so just do a
# cast, we know what's in coredata anyway.
# TODO: if it's possible to annotate get_option or validate_option_value
# in the future we might be able to remove the cast here
return T.cast('T.Union[str, int, bool, WrapMode]', self.options[key].value)
@staticmethod
def parse_overrides(kwargs: T.Dict[str, T.Any]) -> T.Dict[OptionKey, str]:
opts = kwargs.get('override_options', [])
# In this case we have an already parsed and ready to go dictionary
# provided by typed_kwargs
if isinstance(opts, dict):
return T.cast('T.Dict[OptionKey, str]', opts)
result: T.Dict[OptionKey, str] = {}
overrides = stringlistify(opts)
for o in overrides:
if '=' not in o:
raise InvalidArguments('Overrides must be of form "key=value"')
k, v = o.split('=', 1)
key = OptionKey.from_string(k.strip())
v = v.strip()
result[key] = v
return result
def is_linkable_target(self) -> bool:
return False
def get_outputs(self) -> T.List[str]:
return []
def should_install(self) -> bool:
return False
class BuildTarget(Target):
known_kwargs = known_build_target_kwargs
install_dir: T.List[T.Union[str, Literal[False]]]
# This set contains all the languages a linker can link natively
# without extra flags. For instance, nvcc (cuda) can link C++
# without injecting -lc++/-lstdc++, see
# https://github.com/mesonbuild/meson/issues/10570
_MASK_LANGS: T.FrozenSet[T.Tuple[str, str]] = frozenset([
# (language, linker)
('cpp', 'cuda'),
])
def __init__(
self,
name: str,
subdir: str,
subproject: SubProject,
for_machine: MachineChoice,
sources: T.List['SourceOutputs'],
structured_sources: T.Optional[StructuredSources],
objects: T.List[ObjectTypes],
environment: environment.Environment,
compilers: T.Dict[str, 'Compiler'],
kwargs: T.Dict[str, T.Any]):
super().__init__(name, subdir, subproject, True, for_machine, environment, install=kwargs.get('install', False))
self.all_compilers = compilers
self.compilers: OrderedDict[str, Compiler] = OrderedDict()
self.objects: T.List[ObjectTypes] = []
self.structured_sources = structured_sources
self.external_deps: T.List[dependencies.Dependency] = []
self.include_dirs: T.List['IncludeDirs'] = []
self.link_language = kwargs.get('link_language')
self.link_targets: T.List[LibTypes] = []
self.link_whole_targets: T.List[T.Union[StaticLibrary, CustomTarget, CustomTargetIndex]] = []
self.depend_files: T.List[File] = []
self.link_depends = []
self.added_deps = set()
self.name_prefix_set = False
self.name_suffix_set = False
self.filename = 'no_name'
# The debugging information file this target will generate
self.debug_filename = None
# The list of all files outputted by this target. Useful in cases such
# as Vala which generates .vapi and .h besides the compiled output.
self.outputs = [self.filename]
self.pch: T.Dict[str, T.List[str]] = {}
self.extra_args: T.DefaultDict[str, T.List[str]] = kwargs.get('language_args', defaultdict(list))
self.sources: T.List[File] = []
self.generated: T.List['GeneratedTypes'] = []
self.extra_files: T.List[File] = []
self.d_features: DFeatures = {
'debug': kwargs.get('d_debug', []),
'import_dirs': kwargs.get('d_import_dirs', []),
'versions': kwargs.get('d_module_versions', []),
'unittest': kwargs.get('d_unittest', False),
}
self.pic = False
self.pie = False
# Track build_rpath entries so we can remove them at install time
self.rpath_dirs_to_remove: T.Set[bytes] = set()
self.process_sourcelist(sources)
# Objects can be:
# 1. Preexisting objects provided by the user with the `objects:` kwarg
# 2. Compiled objects created by and extracted from another target
self.process_objectlist(objects)
self.process_kwargs(kwargs)
self.missing_languages = self.process_compilers()
# self.link_targets and self.link_whole_targets contains libraries from
# dependencies (see add_deps()). They have not been processed yet because
# we have to call process_compilers() first and we need to process libraries
# from link_with and link_whole first.
# See https://github.com/mesonbuild/meson/pull/11957#issuecomment-1629243208.
link_targets = extract_as_list(kwargs, 'link_with') + self.link_targets
link_whole_targets = extract_as_list(kwargs, 'link_whole') + self.link_whole_targets
self.link_targets.clear()
self.link_whole_targets.clear()
self.link(link_targets)
self.link_whole(link_whole_targets)
if not any([self.sources, self.generated, self.objects, self.link_whole_targets, self.structured_sources,
kwargs.pop('_allow_no_sources', False)]):
mlog.warning(f'Build target {name} has no sources. '
'This was never supposed to be allowed but did because of a bug, '
'support will be removed in a future release of Meson')
self.check_unknown_kwargs(kwargs)
self.validate_install()
self.check_module_linking()
def post_init(self) -> None:
''' Initialisations and checks requiring the final list of compilers to be known
'''
self.validate_sources()
if self.structured_sources and any([self.sources, self.generated]):
raise MesonException('cannot mix structured sources and unstructured sources')
if self.structured_sources and 'rust' not in self.compilers:
raise MesonException('structured sources are only supported in Rust targets')
if self.uses_rust():
# relocation-model=pic is rustc's default and Meson does not
# currently have a way to disable PIC.
self.pic = True
if 'vala' in self.compilers and self.is_linkable_target():
self.outputs += [self.vala_header, self.vala_vapi]
self.install_tag += ['devel', 'devel']
if self.vala_gir:
self.outputs.append(self.vala_gir)
self.install_tag.append('devel')
def __repr__(self):
repr_str = "<{0} {1}: {2}>"
return repr_str.format(self.__class__.__name__, self.get_id(), self.filename)
def __str__(self):
return f"{self.name}"
@property
def is_unity(self) -> bool:
unity_opt = self.get_option(OptionKey('unity'))
return unity_opt == 'on' or (unity_opt == 'subprojects' and self.subproject != '')
def validate_install(self):
if self.for_machine is MachineChoice.BUILD and self.install:
if self.environment.is_cross_build():
raise InvalidArguments('Tried to install a target for the build machine in a cross build.')
else:
mlog.warning('Installing target build for the build machine. This will fail in a cross build.')
def check_unknown_kwargs(self, kwargs):
# Override this method in derived classes that have more
# keywords.
self.check_unknown_kwargs_int(kwargs, self.known_kwargs)
def check_unknown_kwargs_int(self, kwargs, known_kwargs):
unknowns = []
for k in kwargs:
if k == 'language_args':
continue
if k not in known_kwargs:
unknowns.append(k)
if len(unknowns) > 0:
mlog.warning('Unknown keyword argument(s) in target {}: {}.'.format(self.name, ', '.join(unknowns)))
def process_objectlist(self, objects):
assert isinstance(objects, list)
for s in objects:
if isinstance(s, (str, File, ExtractedObjects)):
self.objects.append(s)
elif isinstance(s, (CustomTarget, CustomTargetIndex, GeneratedList)):
non_objects = [o for o in s.get_outputs() if not is_object(o)]
if non_objects:
raise InvalidArguments(f'Generated file {non_objects[0]} in the \'objects\' kwarg is not an object.')
self.generated.append(s)
else:
raise InvalidArguments(f'Bad object of type {type(s).__name__!r} in target {self.name!r}.')
def process_sourcelist(self, sources: T.List['SourceOutputs']) -> None:
"""Split sources into generated and static sources.
Sources can be:
1. Preexisting source files in the source tree (static)
2. Preexisting sources generated by configure_file in the build tree.
(static as they are only regenerated if meson itself is regenerated)
3. Sources files generated by another target or a Generator (generated)
"""
added_sources: T.Set[File] = set() # If the same source is defined multiple times, use it only once.
for s in sources:
if isinstance(s, File):
if s not in added_sources:
self.sources.append(s)
added_sources.add(s)
elif isinstance(s, (CustomTarget, CustomTargetIndex, GeneratedList)):
self.generated.append(s)
@staticmethod
def can_compile_remove_sources(compiler: 'Compiler', sources: T.List['FileOrString']) -> bool:
removed = False
for s in sources[:]:
if compiler.can_compile(s):
sources.remove(s)
removed = True
return removed
def process_compilers_late(self):
"""Processes additional compilers after kwargs have been evaluated.
This can add extra compilers that might be required by keyword
arguments, such as link_with or dependencies. It will also try to guess
which compiler to use if one hasn't been selected already.
"""
for lang in self.missing_languages:
self.compilers[lang] = self.all_compilers[lang]
# did user override clink_langs for this target?
link_langs = [self.link_language] if self.link_language else clink_langs
# If this library is linked against another library we need to consider
# the languages of those libraries as well.
if self.link_targets or self.link_whole_targets:
for t in itertools.chain(self.link_targets, self.link_whole_targets):
if isinstance(t, (CustomTarget, CustomTargetIndex)):
continue # We can't know anything about these.
for name, compiler in t.compilers.items():
if name in link_langs and name not in self.compilers:
self.compilers[name] = compiler
if not self.compilers:
# No source files or parent targets, target consists of only object
# files of unknown origin. Just add the first clink compiler
# that we have and hope that it can link these objects
for lang in link_langs:
if lang in self.all_compilers:
self.compilers[lang] = self.all_compilers[lang]
break
# Now that we have the final list of compilers we can sort it according
# to clink_langs and do sanity checks.
self.compilers = OrderedDict(sorted(self.compilers.items(),
key=lambda t: sort_clink(t[0])))
self.post_init()
def process_compilers(self) -> T.List[str]:
'''
Populate self.compilers, which is the list of compilers that this
target will use for compiling all its sources.
We also add compilers that were used by extracted objects to simplify
dynamic linker determination.
Returns a list of missing languages that we can add implicitly, such as
C/C++ compiler for cython.
'''
missing_languages: T.List[str] = []
if not any([self.sources, self.generated, self.objects, self.structured_sources]):
return missing_languages
# Preexisting sources
sources: T.List['FileOrString'] = list(self.sources)
generated = self.generated.copy()
if self.structured_sources:
for v in self.structured_sources.sources.values():
for src in v:
if isinstance(src, (str, File)):
sources.append(src)
else:
generated.append(src)
# All generated sources
for gensrc in generated:
for s in gensrc.get_outputs():
# Generated objects can't be compiled, so don't use them for
# compiler detection. If our target only has generated objects,
# we will fall back to using the first c-like compiler we find,
# which is what we need.
if not is_object(s):
sources.append(s)
for d in self.external_deps:
for s in d.sources:
if isinstance(s, (str, File)):
sources.append(s)
# Sources that were used to create our extracted objects
for o in self.objects:
if not isinstance(o, ExtractedObjects):
continue
compsrcs = o.classify_all_sources(o.srclist, [])
for comp in compsrcs:
# Don't add Vala sources since that will pull in the Vala
# compiler even though we will never use it since we are
# dealing with compiled C code.
if comp.language == 'vala':
continue
if comp.language not in self.compilers:
self.compilers[comp.language] = comp
if sources:
# For each source, try to add one compiler that can compile it.
#
# If it has a suffix that belongs to a known language, we must have
# a compiler for that language.
#
# Otherwise, it's ok if no compilers can compile it, because users
# are expected to be able to add arbitrary non-source files to the
# sources list
for s in sources:
for lang, compiler in self.all_compilers.items():
if compiler.can_compile(s):
if lang not in self.compilers:
self.compilers[lang] = compiler
break
else:
if is_known_suffix(s):