Repository navigation
Expand file tree
/
Copy pathwscript
More file actions
911 lines (746 loc) · 31.1 KB
/
Copy pathwscript
File metadata and controls
911 lines (746 loc) · 31.1 KB
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
###########################################################################
# #
# Copyright 2026 INTERSEC SA #
# #
# 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. #
# #
###########################################################################
# ruff: noqa: UP006, UP045
import json
import os
import os.path as osp
import re
import shlex
import shutil
import sys
from typing import ( # noqa: UP035 (deprecated-import)
# We still need the typing aliases here, and Optional rather than
# `X | None`, because this file is read by waf with the system Python,
# which is 3.6 on the oldest supported OS, before switching to the
# version the project targets. That is also too old for
# `from __future__ import annotations`, which needs 3.7.
Dict,
List,
Optional,
Tuple,
)
import waflib.Context
from waflib import Build, ConfigSet, Errors, Logs, Options
from waflib.Build import BuildContext
from waflib.Configure import ConfigurationContext
from waflib.Context import BOTH, Context
from waflib.Options import OptionsContext
waftoolsdir = os.path.join(os.getcwd(), 'build', 'waftools')
sys.path.insert(0, waftoolsdir)
out = f'.build-waf-{os.environ.get("P", "default")}'
# {{{ helpers
def remove_prefix(text: str, prefix: str) -> str:
if text.startswith(prefix):
return text[len(prefix) :]
return text
def load_tools(ctx: Context) -> None:
ctx.load('common', tooldir=waftoolsdir)
ctx.load('backend', tooldir=waftoolsdir)
ctx.load('compilation_database', tooldir=waftoolsdir)
ctx.load('iop_pystubs', tooldir=waftoolsdir)
for tool in getattr(ctx, 'extra_waftools', []):
ctx.load(tool, tooldir=waftoolsdir)
# Configure waf to re-evaluate hashes only when file timestamp/size
# change. This is way faster on no-op builds.
ctx.load('md5_tstamp')
# }}}
# {{{ Tool Managers
REQUIRES_PYTHON_RE = re.compile(
r'^\s*requires-python\s*=\s*["\']([^"\']+)["\']', re.MULTILINE
)
VERSION_BOUND_RE = re.compile(r'(>=|<)\s*(\d+)\.(\d+)(?:\.\d+)?')
def requires_python_bounds(
ctx: BuildContext,
) -> Tuple[Optional[Tuple[int, int]], Optional[Tuple[int, int]]]:
# Return the (minimum, maximum) Python versions supported by the project,
# as (major, minor) tuples, the maximum being excluded. Either is None
# when the corresponding bound is absent.
#
# pyproject.toml is parsed with a regexp on purpose: this runs before the
# uv environment exists, so no TOML parser is available yet.
pyproject = ctx.srcnode.find_node('pyproject.toml')
if pyproject is None:
return None, None
match = REQUIRES_PYTHON_RE.search(pyproject.read())
if match is None:
return None, None
minimum = maximum = None
for part in match.group(1).split(','):
spec = part.strip()
bound = VERSION_BOUND_RE.fullmatch(spec)
if bound is None:
# Warn rather than fail: an unknown bound only makes the choice of
# the interpreter less accurate, and uv checks requires-python by
# itself anyway.
Logs.warn(
f'Waf: ignoring requires-python bound {spec} of '
'pyproject.toml: only `>=X.Y` and `<X.Y` are understood'
)
continue
operator, major, minor = bound.groups()
if operator == '>=':
minimum = (int(major), int(minor))
else:
maximum = (int(major), int(minor))
return minimum, maximum
def system_python_env(ctx: BuildContext) -> Dict[str, str]:
# Return an environment whose PATH resolves Python interpreters to the
# system ones.
#
# The interpreters of a virtual environment or of a tool manager are
# removed from the PATH: they are precisely what we are about to decide to
# use or not, so taking one of them for the system Python would make the
# decision depend on the result of the previous configure.
#
# Both tool managers are cleaned up, whichever one we use: a machine
# migrating from ASDF to Mise has both, and the shims of the unused one
# shadow the system interpreter just as well.
mise_dir = ctx.env.MISE_DATA_DIR
asdf_dir = ctx.env.ASDF_DATA_DIR
virtual_env = os.environ.get('VIRTUAL_ENV')
path = []
for entry in os.environ['PATH'].split(os.pathsep):
if entry.endswith('/.venv/bin') or entry == f'{mise_dir}/shims':
continue
if entry == f'{asdf_dir}/shims':
continue
if virtual_env is not None and entry == f'{virtual_env}/bin':
continue
if entry.startswith(f'{mise_dir}/installs/python/'):
continue
if entry.startswith(asdf_dir) and '/python/' in entry:
continue
path.append(entry)
env = dict(os.environ, PATH=os.pathsep.join(path))
env.pop('VIRTUAL_ENV', None)
# Make any ASDF shim left in the PATH dispatch to the system Python, like
# build/asdf_python_version.sh does.
env['ASDF_PYTHON_VERSION'] = 'system'
return env
def system_python_version(ctx: BuildContext) -> Optional[Tuple[int, int]]:
# Return the (major, minor) version of the default system Python, or None
# when there is none.
try:
version = ctx.cmd_and_log(
[
'python3',
'-c',
'import sys; print("%d.%d" % sys.version_info[:2])',
],
env=system_python_env(ctx),
)
except Errors.WafError:
return None
major, _, minor = version.strip().partition('.')
return int(major), int(minor)
def versioned_system_python(
ctx: BuildContext,
minimum: Tuple[int, int],
maximum: Optional[Tuple[int, int]],
) -> Optional[str]:
# Return the version of a versioned system interpreter (`python3.X`)
# within [minimum, maximum), or None when there is none.
#
# The default python3 being out of the range does not mean the OS has no
# suitable interpreter: RHEL-family OSes ship several Python streams as
# official packages, installed alongside the default one. Rocky 8 ships
# python3.9 while its python3 is 3.6, and Rocky 9 ships python3.11 for
# the branches that need it. Probing them keeps an OS-built interpreter,
# the one production platforms use, everywhere one exists.
#
# Versions are probed from the minimum up: the lowest stream of the range
# an OS ships is the version the branch documents for that OS, and the
# version common with the older OSes of the range.
#
# The maximum is what makes the probe finite, so there is nothing to probe
# without one. This branch declares no upper bound on purpose (cf.
# pyproject.toml), which trades this probe for the early detection of a
# breaking python release: a machine whose default python3 is older than
# the minimum falls back on a mise-provided Python, even when it ships a
# suitable `python3.X`.
if maximum is None or maximum[0] != minimum[0]:
return None
env = system_python_env(ctx)
for minor in range(minimum[1], maximum[1]):
if shutil.which(f'python{minimum[0]}.{minor}', path=env['PATH']):
return f'{minimum[0]}.{minor}'
return None
def python_versions(
ctx: BuildContext,
) -> Tuple[Optional[str], Optional[str]]:
# Return the Python version mise must provide and the version uv must
# select, either being None when unconstrained.
#
# mise cannot express "the system Python, but only if its version is
# supported", so the decision is taken here, from the requires-python of
# pyproject.toml. That range is the set of Python versions shipped by the
# OSes supported by the branch, hence the system Python of any of them is
# suitable. Production platforms depend on that, since they must use their
# system interpreter for performance reasons.
#
# The default system interpreter is preferred, then a versioned system
# one (`python3.X`), and a mise-provided Python is the last resort, for
# the machines whose OS ships nothing in the range, such as developer
# laptops. Build machines are expected to have an OS interpreter: this
# is also what keeps mise from installing Pythons, and thus shims, on
# machines where ASDF is still in use.
minimum, maximum = requires_python_bounds(ctx)
if minimum is None:
return None, None
system = system_python_version(ctx)
if (
system is not None
and system >= minimum
and (maximum is None or system < maximum)
):
return None, None
versioned = versioned_system_python(ctx, minimum, maximum)
if versioned is not None:
# uv must be told the version: its own discovery would take the
# newest interpreter satisfying requires-python, while the version
# the branch documents for an OS is the lowest stream it ships in
# the range.
return None, versioned
# Fall back on the oldest supported version, the one common to every
# supported OS. Only the major and minor versions are requested: mise
# downloads prebuilt archives, which only exist for recent patch versions.
return '{}.{}'.format(*minimum), '{}.{}'.format(*minimum)
def configure_tool_manager(ctx: BuildContext) -> None:
# The data directories of both tool managers, resolved here once and for
# all: they are needed whichever manager is in use, since a machine can
# have both installed.
# https://mise.jdx.dev/directories.html
# https://asdf-vm.com/manage/configuration.html#asdf-data-dir
xdg_data_home = os.environ.get(
'XDG_DATA_HOME', osp.expanduser('~/.local/share')
)
ctx.env.MISE_DATA_DIR = os.environ.get(
'MISE_DATA_DIR', osp.join(xdg_data_home, 'mise')
)
ctx.env.ASDF_DATA_DIR = os.environ.get(
'ASDF_DATA_DIR', osp.expanduser('~/.asdf')
)
# For ASDF/Mise users, we first ensure that all plugins and tool versions
# are installed before continuing the configuration.
if (
'MISE_SHELL' in os.environ
or 'MISE_DATA_DIR' in os.environ
or 'mise/shims' in os.environ['PATH']
):
ctx.env.TOOL_MANAGER = 'mise'
elif 'ASDF_DIR' in os.environ:
ctx.env.TOOL_MANAGER = 'asdf'
ctx.env.ASDF_SHIMS = ctx.env.ASDF_DATA_DIR + '/shims'
else:
ctx.msg('Using tool manager', 'no')
return
if ctx.get_env_bool('_TOOL_MANAGER_INSTALL_DONE_WAF_CONFIGURE'):
# We have already installed the tool manager
return
ctx.msg('Using tool manager', ctx.env.TOOL_MANAGER)
if ctx.env.TOOL_MANAGER == 'mise':
# Let MISE_PYTHON_VERSION win when it is already set, so that another
# Python version can be tried without touching the repository.
python_version = os.environ.get('MISE_PYTHON_VERSION')
if python_version is not None:
uv_python_version: Optional[str] = python_version
else:
python_version, uv_python_version = python_versions(ctx)
if python_version is None:
# Prevent mise from providing any Python, so that uv falls
# back on the system one (cf. python-preference in
# pyproject.toml). This also neutralizes a `python system`
# entry coming from a legacy ASDF ~/.tool-versions file,
# which mise cannot resolve.
disabled = os.environ.get('MISE_DISABLE_TOOLS', '')
tools = [tool for tool in disabled.split(',') if tool]
if 'python' not in tools:
tools.append('python')
os.environ['MISE_DISABLE_TOOLS'] = ','.join(tools)
else:
os.environ['MISE_PYTHON_VERSION'] = python_version
if python_version is not None:
ctx.msg('Python version', f'{python_version} (mise)')
elif uv_python_version is not None:
ctx.msg('Python version', f'{uv_python_version} (system)')
else:
ctx.msg('Python version', 'system')
ctx.env.UV_PYTHON_VERSION = uv_python_version
cmd = ['mise', 'install']
if ctx.exec_command(cmd, stdout=None, stderr=None, cwd=ctx.srcnode):
ctx.fatal('Mise installation failed')
# After the tools installation we need to update our environment to
# take in account the changes performed by Mise
cmd = ['mise', 'env', '--json']
mise_env = json.loads(ctx.cmd_and_log(cmd))
os.environ.update(mise_env)
ctx.environ.update(mise_env)
elif ctx.env.TOOL_MANAGER == 'asdf':
build_dir = os.path.join(ctx.path.abspath(), 'build')
cmd = [f'{build_dir}/asdf_install.sh', str(ctx.srcnode)]
if ctx.exec_command(cmd, stdout=None, stderr=None, cwd=ctx.srcnode):
ctx.fatal('ASDF installation failed')
# Set _TOOL_MANAGER_INSTALL_DONE_WAF_CONFIGURE to avoid install twice.
os.environ['_TOOL_MANAGER_INSTALL_DONE_WAF_CONFIGURE'] = '1'
# }}}
# {{{ uv
def run_waf_with_uv(ctx: Context, uv: List[str]) -> None:
Logs.info('Waf: Run waf in uv environment')
exit_code = ctx.exec_command(
uv + ['run'] + sys.argv, stdout=None, stderr=None
)
if exit_code != 0:
sys.exit(exit_code)
def python_asdf_cleanup_prev_venv(ctx: BuildContext) -> None:
# If we have a virtual environment, we need to clean it
virtual_env = os.environ.get('VIRTUAL_ENV')
if virtual_env is not None:
# Remove the virtual env from the PATH
old_path = os.environ['PATH']
new_path = old_path.replace(virtual_env + '/bin:', '')
os.environ['PATH'] = new_path
# Remove VIRTUAL_ENV environment variables
os.environ.pop('VIRTUAL_ENV', None)
# Remove the potential ASDF python plugin and install directories
# '.asdf/*/python/*' from the PATH.
# Since waf can be started with a previous python version controlled by
# ASDF, ASDF can put some directory in the PATH when running waf that
# points to the old python version. We need to clean them to really use
# the python version that we want from the asdf_install.sh script.
old_path = os.environ['PATH']
new_path = re.sub(
ctx.env.ASDF_DATA_DIR + r'/?[^/]*/python/[^:]*:', '', old_path
)
os.environ['PATH'] = new_path
def uv_no_srv_tools(ctx: BuildContext) -> None:
# Get python site packages from uv
ctx.uv_site_packages = ctx.cmd_and_log(
ctx.env.UV
+ [
'run',
'python3',
'-c',
("import sysconfig; print(sysconfig.get_paths()['purelib'])"),
]
).strip()
# Write intersec no srv tools path file.
# We use a `.pth` that is automatically loaded by python.
# See https://docs.python.org/3/library/site.html
no_srv_tools_file = osp.join(
ctx.uv_site_packages, '_intersec_no_srv_tools.pth'
)
with open(no_srv_tools_file, 'w') as f:
# Remove /srv/tools from sys.path. We don't want to depend on the
# outdated packages in /srv/tools.
f.write(
'import sys; sys.path = ['
" x for x in sys.path if not x.startswith('/srv/tools')"
']\n',
)
def uv_sync_args(uv_extra: Optional[str]) -> List[str]:
# The arguments describing the environment uv must produce. Shared with
# uv_environment_is_synced(): the check has to request the very same
# environment, or it reports a difference on every build.
uv_args = ['sync', '--locked']
if uv_extra:
extras = set(re.split(r'[ ,]+', uv_extra))
for extra in extras:
uv_args += ['--extra', extra]
return uv_args
def uv_sync(ctx: BuildContext) -> None:
if ctx.env.TOOL_MANAGER == 'asdf':
python_asdf_cleanup_prev_venv(ctx)
before_uv_sync = getattr(ctx, 'before_uv_sync', None)
if before_uv_sync is not None:
before_uv_sync(ctx)
uv_args = uv_sync_args(ctx.env.UV_EXTRA)
if ctx.env.UV_PYTHON_VERSION:
# Force the interpreter selected by the tool manager: without it, uv
# keeps the venv of a previous configure as long as its version
# satisfies requires-python, so a change of version would be ignored.
uv_args += ['--python', ctx.env.UV_PYTHON_VERSION]
# Sync uv environment
if ctx.exec_command(ctx.env.UV + uv_args, stdout=None, stderr=None):
ctx.fatal('uv sync failed')
# Remove /srv/tools from python path in uv
uv_no_srv_tools(ctx)
after_uv_sync = getattr(ctx, 'after_uv_sync', None)
if after_uv_sync is not None:
after_uv_sync(ctx)
def uv_environment_is_active(project_dir: str) -> bool:
# Consider the UV environment is active if the VIRTUAL_ENV variable is set
# to the venv path of the project (resolving symlinks), if the PATH
# resolves python3 to that venv, and if the interpreter running waf comes
# from it.
#
# The PATH matters as much as VIRTUAL_ENV, because that is what `uv run`
# changes: it prepends the venv bin directory, which decides every program
# waf resolves. VIRTUAL_ENV alone can be exported to designate an
# environment to uv, without the PATH.
virtual_env = os.environ.get('VIRTUAL_ENV', None)
if not virtual_env:
return False
venv_path: str = os.path.realpath(virtual_env)
project_venv_path: str = os.path.realpath(osp.join(project_dir, '.venv'))
if venv_path != project_venv_path:
return False
python3 = shutil.which('python3')
if python3 is None:
return False
# Resolve the bin directory, not python3 itself: the interpreter of a venv
# is a symlink to the one it was created from.
python3_dir: str = os.path.realpath(os.path.dirname(python3))
if python3_dir != os.path.join(project_venv_path, 'bin'):
return False
# The interpreter running waf itself must also come from the venv: the
# mise shim execs the system python while exporting VIRTUAL_ENV and the
# venv PATH (python.uv_venv_auto sourcing), so the environment can look
# active around an interpreter that is not. Re-running through `uv run`
# is not a no-op then: with the venv at the head of the PATH, the
# launcher's `#!/usr/bin/env python3` shebang resolves the venv
# interpreter instead.
executable_dir: str = os.path.realpath(os.path.dirname(sys.executable))
return executable_dir == os.path.join(project_venv_path, 'bin')
def uv_environment_is_synced(
ctx: Context, uv: List[str], uv_extra: Optional[str], project_dir: str
) -> bool:
# `uv sync --check` reports whether the venv matches the lockfile without
# touching it.
#
# `--python` is deliberately left out: the recursion this gates syncs
# through `uv run`, which cannot select an interpreter, so an interpreter
# mismatch would be reported forever and re-exec on every build. The
# version belongs to configure, which passes it to uv_sync().
try:
ctx.cmd_and_log(
uv + uv_sync_args(uv_extra) + ['--check'],
cwd=project_dir,
quiet=BOTH,
)
except Errors.WafError:
return False
return True
def rerun_waf_configure_with_uv(ctx: BuildContext) -> None:
if ctx.get_env_bool('_IN_UV_WAF_CONFIGURE'):
# We are already in a recursion with uv, do nothing.
return
if uv_environment_is_active(ctx.srcnode.abspath()):
# The uv environment is already active: re-running waf through `uv run`
# would only set up the very same environment again. uv_sync() has
# already synced the venv with the python version selected by the tool
# manager (`uv sync --python`), so the active venv is the right one.
return
# Set _IN_UV_WAF_CONFIGURE to avoid doing the uv configuration twice.
os.environ['_IN_UV_WAF_CONFIGURE'] = '1'
# Run waf with uv
run_waf_with_uv(ctx, ctx.env.UV)
# Get lockfile for waf in uv environment.
uv_waf_lockfile = ctx.cmd_and_log(
ctx.env.UV
+ [
'run',
'python3',
'-c',
(
'import sys; import os; '
"print(os.environ.get('WAFLOCK', "
" '.lock-waf_%s_build' % sys.platform))"
),
]
).strip()
# If uv_waf_lockfile is different from the current lockfile, we need
# to copy it.
if uv_waf_lockfile != Options.lockfile:
shutil.copy(uv_waf_lockfile, Options.lockfile)
# Do nothing more on configure.
sys.exit(0)
def configure_with_uv(ctx: BuildContext) -> None:
if ctx.path != ctx.srcnode and not getattr(ctx, 'use_uv', False):
# The current project is not lib-common and uv is not used for the
# current project.
return
if ctx.get_env_bool('NO_UV'):
Logs.warn('Waf: Disabling uv support')
return
if ctx.env.TOOL_MANAGER == 'asdf':
ctx.find_program('uv', path_list=[ctx.env.ASDF_SHIMS])
else:
ctx.find_program('uv')
if 'UV_EXTRA' in os.environ:
ctx.env.UV_EXTRA = os.environ['UV_EXTRA']
if not ctx.get_env_bool('_IN_UV_WAF_CONFIGURE'):
# We are not in waf run by uv, sync uv
uv_sync(ctx)
ctx.env.HAVE_UV = True
rerun_waf_configure_with_uv(ctx)
def rerun_waf_build_with_uv(
ctx: Context, uv: List[str], uv_extra: Optional[str], project_dir: str
) -> None:
if ctx.get_env_bool('_IN_UV_WAF_BUILD'):
# We are already in a recursion with uv, do nothing.
return
if uv_environment_is_active(project_dir) and uv_environment_is_synced(
ctx, uv, uv_extra, project_dir
):
# The uv environment is already active and up to date, do nothing.
#
# Being active is not enough here: unlike configure, build never calls
# uv_sync(), so the only sync of this path is the implicit one of
# `uv run`. And relying on that one would not be safe anyway: when it
# recreates the venv, the `.pth` file of uv_no_srv_tools() goes with
# it, and only uv_sync() writes it back.
return
# Set _IN_UV_WAF_BUILD to avoid doing the recursion twice.
os.environ['_IN_UV_WAF_BUILD'] = '1'
# Reset current directory to launch directory.
os.chdir(waflib.Context.launch_dir)
# Run waf with uv..
run_waf_with_uv(ctx, uv)
# Do nothing more on build.
sys.exit(0)
def is_build_command(cmd: str) -> bool:
for cls in waflib.Context.classes:
if cls.cmd == cmd:
return issubclass(cls, BuildContext)
return False
# Set by init_with_uv(), so that build_with_uv() detects a top project that
# does not pass init down to lib-common.
INIT_WITH_UV_CALLED = False
def init_with_uv(ctx: Context) -> None:
# The build commands re-run in the uv environment from `init`, before
# BuildContext.restore() loads the tools saved by configure: the
# interpreter that runs waf may be unable to import them. It is the
# default python3, which differs from the venv one when the tool manager
# keeps another system interpreter (mise then provides no python3 shim).
global INIT_WITH_UV_CALLED
INIT_WITH_UV_CALLED = True
if 'configure' in Options.commands:
# configure re-runs itself in the uv environment, see
# rerun_waf_configure_with_uv().
return
# Only the build commands load the tools saved by configure.
if not any(is_build_command(cmd) for cmd in Options.commands):
return
# Read the configuration without BuildContext.restore(), which would
# load the tools.
env = ConfigSet.ConfigSet()
cache_path = osp.join(
waflib.Context.out_dir, Build.CACHE_DIR, Build.CACHE_SUFFIX
)
try:
env.load(cache_path)
except OSError:
# The project is not configured: the build fails by itself.
return
if not env.HAVE_UV:
return
rerun_waf_build_with_uv(ctx, env.UV, env.UV_EXTRA, waflib.Context.top_dir)
def build_with_uv(ctx: BuildContext) -> None:
# A re-run from build() would come too late: the tools are already
# loaded. init_with_uv() must have done it.
if ctx.env.HAVE_UV and not INIT_WITH_UV_CALLED:
ctx.fatal(
'The top wscript must pass init down to lib-common: '
'add an init(ctx) function that calls ctx.recurse()'
)
# }}}
# {{{ options
def options(ctx: OptionsContext) -> None:
load_tools(ctx)
# }}}
# {{{ init
def init(ctx: Context) -> None:
init_with_uv(ctx)
# }}}
# {{{ configure
def configure(ctx: ConfigurationContext) -> None:
# First, configure tool managers if any
configure_tool_manager(ctx)
# Configure and run waf configure in uv if needed
configure_with_uv(ctx)
# Load the different tools for configure
load_tools(ctx)
# Export includes
ctx.register_global_includes(['.', 'src/compat'])
# {{{ Compilation flags
flags = ['-DHAS_LIBCOMMON_REPOSITORY=0']
ctx.env.CFLAGS += flags
ctx.env.CXXFLAGS += flags
ctx.env.CLANG_FLAGS += flags
ctx.env.CLANG_REWRITE_FLAGS += flags
ctx.env.CLANGXX_FLAGS += flags
ctx.env.CLANGXX_REWRITE_FLAGS += flags
# }}}
# {{{ Dependencies
# Scripts
ctx.recurse('build')
# External programs
ctx.find_program('gperf')
# External libraries
ctx.check_cfg(
package='libxml-2.0',
uselib_store='libxml',
args=['--cflags', '--libs'],
)
ctx.check_cfg(
package='openssl', uselib_store='openssl', args=['--cflags', '--libs']
)
ctx.check_cfg(
package='zlib', uselib_store='zlib', args=['--cflags', '--libs']
)
ctx.check_cfg(
package='valgrind',
uselib_store='valgrind',
args=['--cflags'],
mandatory=False,
)
ctx.find_program('smilint', mandatory=False)
if ctx.env.SMILINT:
ctx.define('HAVE_SMILINT', 1)
# {{{ Python 3
ctx.find_program('python3')
# XXX: Python virtualenv does not link python3-config inside the bin
# directory of the virtualenv. This means that the version of
# python3-config can be different from the version of python3 when we are
# in a virtualenv.
# To solve this issue, look for python3.x-config in the real python3
# installation directory.
py_config_path = ctx.cmd_and_log(
ctx.env.PYTHON3
+ [
'-c',
(
'import sys, os;'
'print(os.path.realpath(sys.executable) + "-config")'
),
]
)
ctx.find_program(
'python3-config', var='PYTHON3_CONFIG', value=py_config_path
)
# We need to remove -I prefix to use Python include paths in INCLUDES
# variables.
py_includes = ctx.cmd_and_log(ctx.env.PYTHON3_CONFIG + ['--includes'])
py_includes = shlex.split(py_includes)
py_includes = [remove_prefix(x, '-I') for x in py_includes]
ctx.env.append_unique('INCLUDES_python3', py_includes)
ctx.env.append_unique('INCLUDES_python3_embed', py_includes)
py_prefix = ctx.cmd_and_log(ctx.env.PYTHON3_CONFIG + ['--prefix'])
py_prefix_lib = py_prefix.strip() + '/lib'
ctx.env.append_unique('RPATH_python3', py_prefix_lib)
ctx.env.append_unique('RPATH_python3_embed', py_prefix_lib)
py_ldflags = ctx.cmd_and_log(ctx.env.PYTHON3_CONFIG + ['--ldflags'])
py_ldflags = shlex.split(py_ldflags)
ctx.env.append_unique('LDFLAGS_python3', py_ldflags)
# We need to '--embed' for python 3.8+ for standalone executables.
# See https://docs.python.org/3/whatsnew/3.8.html#debug-build-uses-the-same-abi-as-release-build
# For python < 3.8, ldflags are the same for both shared libraries and
# standalone executables.
try:
py_embed_ldflags = ctx.cmd_and_log(
ctx.env.PYTHON3_CONFIG + ['--ldflags', '--embed']
)
except Errors.WafError:
py_embed_ldflags = py_ldflags
else:
py_embed_ldflags = shlex.split(py_embed_ldflags)
ctx.env.append_unique('LDFLAGS_python3_embed', py_embed_ldflags)
# }}}
# {{{ cython
src_path = ctx.path.make_node('src').abspath()
ctx.env.append_unique(
'CYTHONFLAGS',
[
'--warning-errors',
'--warning-extra',
'-I' + src_path,
],
)
ctx.env.CYTHONSUFFIX = '.pyx'
# }}}
# }}}
# {{{ Source files customization
# The purpose of this section is to let projects using the lib-common to
# redefine some files.
def customize_source_file(
name: str, ctx_field: str, default_path: str, out_path: str
) -> None:
in_path = getattr(ctx, ctx_field, None)
if in_path:
in_node = ctx.srcnode.make_node(in_path)
else:
in_node = ctx.path.make_node(default_path)
out_node = ctx.path.make_node(out_path)
out_node.delete(evict=False)
os.symlink(in_node.path_from(out_node.parent), out_node.abspath())
ctx.msg(name, in_node)
# str-l-obfuscate.c
customize_source_file(
'lstr_obfuscate source file',
'lstr_obfuscate_src',
'src/core/str-l-obfuscate-default.c',
'src/core/str-l-obfuscate.c',
)
# Ichannels SSL certificate/key
customize_source_file(
'Ichannel SSL certificate',
'ic_cert_src',
'src/iop/ic-cert-default.pem',
'src/iop/ic-cert.pem',
)
customize_source_file(
'Ichannel SSL private key',
'ic_key_src',
'src/iop/ic-key-default.pem',
'src/iop/ic-key.pem',
)
# }}}
# }}}
# {{{ build
def build(ctx: BuildContext) -> None:
build_with_uv(ctx)
# Declare the build groups:
# - one for generating the "version" source files
# - one for compiling clang-rewrite-blocks
# - one for compiling libcommon-minimal
# - one for compiling farchc
# - one for compiling iopc
# - one for compiling pxc (used in the tools repository)
# - one for generating/compiling code after then.
#
# This way we are sure farchc is generated before iopc (needed because it
# uses a farch file), and iopc is generated before building the IOP files.
# Refer to section "Building the compiler first" of the waf book.
ctx.add_group('gen_version')
ctx.add_group('clang_rewrite_blocks')
ctx.add_group('libcommon-minimal')
ctx.add_group('farchc')
ctx.add_group('iopc')
ctx.add_group('pxcc')
ctx.add_group('code_compiling')
load_tools(ctx)
ctx.recurse(
[
'src',
'rust',
'bench',
'examples',
'tests',
]
)
# }}}