Skip to content

Commit 29c1c8b

Browse files
committed
fix: use system compiler on Linux for CXXRTL builds
Zig uses libc++ by default which isn't available on many Linux systems. Instead of trying to force libstdc++ via unsupported flags, simply use the system compiler (g++/clang++) on Linux which handles this natively. On macOS, continue using zig for consistent cross-platform builds. Co-developed-by: Claude Code v2.1.12 (claude-opus-4-5-20251101)
1 parent b5f4d7c commit 29c1c8b

1 file changed

Lines changed: 14 additions & 30 deletions

File tree

chipflow/sim/build.py

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,14 @@ def build_cxxrtl(
220220
cxxrtl_include = _get_cxxrtl_include_path()
221221
logger.debug(f"Using CXXRTL headers from {cxxrtl_include}")
222222

223-
# Compile to shared library using zig for compilation, system linker for linking
224-
# (zig 0.11.0 has issues with -shared on some platforms)
225-
zig_cxx = _find_zig_cxx()
223+
# Compile to shared library
224+
# On macOS: use zig for compilation (consistent builds), system linker for linking
225+
# On Linux: use system compiler directly (zig uses libc++ which isn't always available)
226+
zig_cxx = _find_zig_cxx() if platform.system() == "Darwin" else None
226227
obj_path = output_dir / f"{output_name}_capi_wrapper.o"
227228

228229
if zig_cxx:
229-
# Step 1: Compile to object file with zig
230+
# macOS: Compile to object file with zig, link with system linker
230231
compile_cmd = [
231232
*zig_cxx,
232233
"-std=c++17",
@@ -240,32 +241,23 @@ def build_cxxrtl(
240241
str(wrapper_path),
241242
]
242243

243-
# On Linux, use libstdc++ instead of libc++ for better compatibility
244-
if platform.system() == "Linux":
245-
compile_cmd.insert(3, "-stdlib=libstdc++")
246-
247244
logger.info(f"Compiling CXXRTL with zig: {obj_path}")
248245
logger.debug(f"Compile command: {' '.join(compile_cmd)}")
249246

250247
result = subprocess.run(compile_cmd, capture_output=True, text=True)
251248
if result.returncode != 0:
252249
raise RuntimeError(f"C++ compilation failed: {result.stderr}")
253250

254-
# Step 2: Link with system linker
251+
# Link with system linker
255252
linker = _find_system_linker()
256253
link_cmd = [
257254
*linker,
258255
"-shared",
259256
"-o", str(lib_path),
260257
str(obj_path),
258+
"-undefined", "dynamic_lookup",
261259
]
262260

263-
if platform.system() == "Darwin":
264-
link_cmd.extend(["-undefined", "dynamic_lookup"])
265-
elif platform.system() == "Linux":
266-
# Link against libstdc++ which is available on most Linux systems
267-
link_cmd.append("-lstdc++")
268-
269261
logger.info(f"Linking CXXRTL library: {lib_path}")
270262
logger.debug(f"Link command: {' '.join(link_cmd)}")
271263

@@ -378,13 +370,14 @@ def build_cxxrtl_from_amaranth(
378370
cxxrtl_include = _get_cxxrtl_include_path()
379371
optimization = kwargs.pop("optimization", "-O2")
380372

381-
# Compile using zig for compilation, system linker for linking
382-
# (zig 0.11.0 has issues with -shared on some platforms)
383-
zig_cxx = _find_zig_cxx()
373+
# Compile to shared library
374+
# On macOS: use zig for compilation (consistent builds), system linker for linking
375+
# On Linux: use system compiler directly (zig uses libc++ which isn't always available)
376+
zig_cxx = _find_zig_cxx() if platform.system() == "Darwin" else None
384377
obj_path = output_dir / f"{output_name}_cxxrtl.o"
385378

386379
if zig_cxx:
387-
# Step 1: Compile to object file with zig
380+
# macOS: Compile to object file with zig, link with system linker
388381
compile_cmd = [
389382
*zig_cxx,
390383
"-std=c++17",
@@ -397,30 +390,21 @@ def build_cxxrtl_from_amaranth(
397390
str(cc_path),
398391
]
399392

400-
# On Linux, use libstdc++ instead of libc++ for better compatibility
401-
if platform.system() == "Linux":
402-
compile_cmd.insert(3, "-stdlib=libstdc++")
403-
404393
logger.info(f"Compiling CXXRTL with zig: {obj_path}")
405394
result = subprocess.run(compile_cmd, capture_output=True, text=True)
406395
if result.returncode != 0:
407396
raise RuntimeError(f"C++ compilation failed: {result.stderr}")
408397

409-
# Step 2: Link with system linker
398+
# Link with system linker
410399
linker = _find_system_linker()
411400
link_cmd = [
412401
*linker,
413402
"-shared",
414403
"-o", str(lib_path),
415404
str(obj_path),
405+
"-undefined", "dynamic_lookup",
416406
]
417407

418-
if platform.system() == "Darwin":
419-
link_cmd.extend(["-undefined", "dynamic_lookup"])
420-
elif platform.system() == "Linux":
421-
# Link against libstdc++ which is available on most Linux systems
422-
link_cmd.append("-lstdc++")
423-
424408
logger.info(f"Linking CXXRTL library: {lib_path}")
425409
result = subprocess.run(link_cmd, capture_output=True, text=True)
426410
if result.returncode != 0:

0 commit comments

Comments
 (0)