11#!/usr/bin/env python3
22"""
3- Generate pkg‑ config (.pc) files for Geant4 and (optionally) ROOT.
3+ Generate pkg- config (.pc) files for Geant4 and (optionally) ROOT.
44
55Usage:
6- ./generate_pc.py <install‑ prefix>
6+ ./generate_pc.py <install- prefix>
77
88Example:
99 ./generate_pc.py /opt/gemc
1010"""
1111import subprocess
1212import shutil
1313import sys
14+ import platform
1415from pathlib import Path
1516from typing import List , Optional
1617
1718print (f"[debug] Python version: { sys .version } " )
1819print (f"[debug] Python executable: { sys .executable } " )
19-
20+ print ( f"[debug] Detected machine: { platform . machine () } " )
2021
2122# ────────────────────────── helpers ──────────────────────────
2223def run_config (command : str , option : str ) -> str :
@@ -33,19 +34,35 @@ def run_config(command: str, option: str) -> str:
3334 raise
3435
3536
37+ def is_arm64_host () -> bool :
38+ """Return True on arm64/aarch64 hosts."""
39+ m = platform .machine ().lower ()
40+ return m in ("aarch64" , "arm64" )
41+
42+
43+ def strip_arch_unsupported_flags (flags : str ) -> str :
44+ """
45+ On arm64 hosts, remove x86-specific -m* width flags that break compiles.
46+ (Some ROOT builds on Debian/Ubuntu inject '-m64' into --cflags.)
47+ """
48+ toks = flags .split ()
49+ if is_arm64_host ():
50+ toks = [t for t in toks if t not in ("-m64" , "-m32" )]
51+ return " " .join (toks )
52+
53+
3654def filter_unwanted_flags (flags : str ) -> str :
3755 """Strip out Qt / CLHEP / XercesC / TreePlayer / -Wshadow flags."""
38- unwanted = {"Qt" , "qt" , "CLHEP" , "clhep" ,
39- "xercesc" , "XercesC" , "TreePlayer" , "-Wshadow" }
40- return " " .join (f for f in flags .split ()
41- if not any (uw in f for uw in unwanted ))
56+ unwanted = {"Qt" , "qt" , "CLHEP" , "clhep" , "xercesc" , "XercesC" , "TreePlayer" , "-Wshadow" }
57+ return " " .join (f for f in flags .split () if not any (uw in f for uw in unwanted ))
4258
4359
44- def filter_root_flags (flags : str , root_libs : list [str ]) -> str :
60+ def filter_root_flags (flags : str , root_libs : List [str ]) -> str :
4561 """Keep only -l<lib> entries matching *root_libs* plus any -L paths."""
46- return " " .join (f for f in flags .split ()
47- if f .startswith ("-L" ) or
48- any (f"-l{ lib } " in f for lib in root_libs ))
62+ return " " .join (
63+ f for f in flags .split ()
64+ if f .startswith ("-L" ) or any (f"-l{ lib } " in f for lib in root_libs )
65+ )
4966
5067
5168def generate_pkgconfig (install_prefix : Path ,
@@ -55,11 +72,15 @@ def generate_pkgconfig(install_prefix: Path,
5572 description : str ,
5673 root_lbs : Optional [List [str ]] = None ) -> None :
5774 """Create <install_prefix>/lib/pkgconfig/<output_filename>."""
58- prefix = run_config (config_cmd , "--prefix" )
59- libs = filter_unwanted_flags (run_config (config_cmd , "--libs" ))
60- cflags = filter_unwanted_flags (run_config (config_cmd , "--cflags" ))
75+ prefix = run_config (config_cmd , "--prefix" )
76+ libs = filter_unwanted_flags (run_config (config_cmd , "--libs" ))
77+ cflags = filter_unwanted_flags (run_config (config_cmd , "--cflags" ))
6178 version = run_config (config_cmd , "--version" )
6279
80+ # Remove x86-only width flags on arm64 hosts (applies to both cflags and libs, just in case)
81+ cflags = strip_arch_unsupported_flags (cflags )
82+ libs = strip_arch_unsupported_flags (libs )
83+
6384 if config_cmd == "root-config" and root_lbs :
6485 libs = filter_root_flags (libs , root_lbs )
6586
@@ -90,18 +111,23 @@ def check_root_config() -> bool:
90111if __name__ == "__main__" :
91112 # --- 1. parse argument ----------------------------------------------------
92113 if len (sys .argv ) != 2 :
93- sys .exit (f"Usage: { sys .argv [0 ]} <install‑ prefix>" )
114+ sys .exit (f"Usage: { sys .argv [0 ]} <install- prefix>" )
94115
95116 install_dir = Path (sys .argv [1 ]).expanduser ().resolve ()
96117 if not install_dir .exists ():
97118 print (f"Creating installation directory { install_dir } " )
98119 install_dir .mkdir (parents = True , exist_ok = True )
99120
100121 # --- 2. generate .pc files -----------------------------------------------
101- #generate_pkgconfig(install_dir, "geant4-config",
102- # "geant4.pc", "Geant4", "Geant4 Simulation Toolkit")
122+ # generate_pkgconfig(install_dir, "geant4-config",
123+ # "geant4.pc", "Geant4", "Geant4 Simulation Toolkit")
103124
104125 if check_root_config ():
105- generate_pkgconfig (install_dir , "root-config" ,
106- "root.pc" , "ROOT" , "ROOT Data Analysis Framework" ,
107- root_lbs = ["RIO" , "Tree" , "Core" , "root" ])
126+ generate_pkgconfig (
127+ install_dir ,
128+ "root-config" ,
129+ "root.pc" ,
130+ "ROOT" ,
131+ "ROOT Data Analysis Framework" ,
132+ root_lbs = ["RIO" , "Tree" , "Core" , "root" ],
133+ )
0 commit comments