forked from JeffersonLab/iguana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolve-dependencies.py
executable file
·133 lines (124 loc) · 5.72 KB
/
resolve-dependencies.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
#!/usr/bin/env python3
from configparser import ConfigParser
import argparse, os, sys, textwrap
# constants
SYSTEM_ASSUMPTION = 'assume system installation'
NOT_USED = 'not used'
# parse user options
class Formatter(argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter): pass
parser = argparse.ArgumentParser(
usage = f'{sys.argv[0]} [OPTION]...',
description = textwrap.dedent('''
description:
Generate build options for resolving dependencies
'''),
formatter_class = Formatter
)
parser_deps = parser.add_argument_group('dependency installation paths')
parser_deps.add_argument('--hipo', default=SYSTEM_ASSUMPTION, type=str, help='path to `hipo` installation')
parser_deps.add_argument('--fmt', default=SYSTEM_ASSUMPTION, type=str, help='path to `fmt` installation')
parser_deps.add_argument('--yaml', default=SYSTEM_ASSUMPTION, type=str, help='path to `yaml-cpp` installation')
parser_deps.add_argument('--root', default=SYSTEM_ASSUMPTION, type=str, help='path to `ROOT` installation')
parser_output = parser.add_argument_group('output control')
parser_output.add_argument('--cli', default=False, action=argparse.BooleanOptionalAction, help='only print the `meson` CLI options, and nothing else')
parser_output.add_argument('--env', default=False, action=argparse.BooleanOptionalAction, help='generate environment variable `export` commands instead')
parser_output.add_argument('--ini', default=NOT_USED, type=str, help='if set, generate an INI file (meson native file) with this name; you may then use it with `meson setup --native-file=_____`')
args = parser.parse_args()
# verbosity
verbose = not args.cli
def print_verbose(message):
if(verbose):
print(message)
# functions to set dependency paths
pkg_config_path = set()
cmake_prefix_path = set()
def use_system(dep):
print_verbose(f'{dep}: {SYSTEM_ASSUMPTION}')
def use_pkg_config(dep, pc_file, arg):
if(arg != SYSTEM_ASSUMPTION):
prefix = os.path.realpath(arg)
pc_path = ''
for root, dirs, files in os.walk(prefix):
if pc_file in files:
# be sure to choose the INSTALLED .pc file; FIXME: may not work for all dependencies, but so far this is okay
if root.split('/')[-1] == 'pkgconfig':
pc_path = root
break
if pc_path == '':
print(f'ERROR: cannot find "{pc_file}" in any subdirectory of {arg}', file=sys.stderr)
exit(1)
print_verbose(f'{dep}: using pkg-config files from {pc_path}')
pkg_config_path.add(pc_path)
else:
use_system(dep)
def use_cmake(dep, arg):
if(arg != SYSTEM_ASSUMPTION):
path = os.path.realpath(arg)
print_verbose(f'{dep}: using cmake files from {path}')
cmake_prefix_path.add(path)
else:
use_system(dep)
# resolve dependencies #########################
use_pkg_config('hipo', 'hipo4.pc', args.hipo)
use_pkg_config('fmt', 'fmt.pc', args.fmt)
use_pkg_config('yaml', 'yaml-cpp.pc', args.yaml)
use_cmake('ROOT', args.root)
################################################
# generate a native file
if(args.ini!=NOT_USED):
def ini_string_arr(arr):
contents = ','.join(map(lambda s: f'\'{s}\'', arr))
return f'[{contents}]'
ini_config = ConfigParser(allow_no_value=True)
ini_config.add_section('built-in options')
if(len(cmake_prefix_path) > 0):
ini_config.set('built-in options', 'cmake_prefix_path', ini_string_arr(cmake_prefix_path))
if(len(pkg_config_path) > 0):
ini_config.set('built-in options', 'pkg_config_path', ini_string_arr(pkg_config_path))
with open(args.ini, 'w') as fp:
ini_config.write(fp)
# generate CLI options
if(verbose or args.cli):
if(len(pkg_config_path)==0 and len(cmake_prefix_path)==0):
print_verbose(textwrap.dedent(f'''
==========================================================================================
All of your dependencies are assumed to be in the system default locations.
- If they are not, please run:
{sys.argv[0]} --help
- Otherwise, you do not need to set or modify any build options for dependency resolution.
==========================================================================================
'''))
exit(0)
else:
if(args.env):
print_verbose(textwrap.dedent('''
==================================================
| Here are the environment variables you need: |
==================================================
'''))
else:
print_verbose(textwrap.dedent('''
===============================================
| Here are the build options that you need: |
===============================================
'''))
cli_opts = []
if(len(pkg_config_path) > 0):
if(args.env):
cli_opts.append(f'export PKG_CONFIG_PATH={":".join(pkg_config_path)}' + '${PKG_CONFIG_PATH:+:${PKG_CONFIG_PATH}}')
else:
cli_opts.append(f'--pkg-config-path={",".join(pkg_config_path)}')
if(len(cmake_prefix_path) > 0):
if(args.env):
cli_opts.append(f'export CMAKE_PREFIX_PATH={":".join(cmake_prefix_path)}' + '${CMAKE_PREFIX_PATH:+:${CMAKE_PREFIX_PATH}}')
else:
cli_opts.append(f'--cmake-prefix-path={",".join(cmake_prefix_path)}')
if(args.ini==NOT_USED):
if(args.env):
for cli_opt in cli_opts:
print(cli_opt)
else:
print(f'{" ".join(cli_opts)}')
else:
print(f'--native-file={args.ini}')
print_verbose('\n')