-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_conan.py
executable file
·248 lines (181 loc) · 6.16 KB
/
run_conan.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
#!/usr/bin/env python
# Copyright (C) 2024 Roberto Rossini <[email protected]>
#
# SPDX-License-Identifier: MIT
import os
import platform
import re
import shutil
import subprocess as sp
import sys
"""
This script takes care of generating a valid conan profile and running conan install.
Most of the complexity is due to Windows, as in this case we must use the compilers provided by the Rtools package.
This means that:
- We need to figure out Rtools' home folder
- Inject the several bin folders into PATH
- Detect compiler and cmake version
- Make conan use the msys2 toolchain installed by Rtools
"""
def find_conan() -> str:
conan = shutil.which("conan")
if conan is None:
raise RuntimeError(
"Unable to find conan in your PATH.\n"
"Please install conan: https://conan.io/downloads"
)
return conan
def get_conan_home() -> str:
return os.environ.get(
"CONAN_HOME", os.path.join(os.path.expanduser("~"), ".conan2")
)
def get_rtools_home() -> str:
res = sp.check_output(
["Rscript", "-e", "package_version(R.version)"], stderr=sp.DEVNULL
).decode("utf-8")
matches = re.search(r"(\d+\.\d+).\d+", res)
if not matches:
raise RuntimeError("Unable to infer R version")
r_version = matches.group(1).replace(".", "")
rtools_string = f"rtools{r_version}"
rtools_home = os.path.join("C:\\", rtools_string)
for p in get_path_as_r(add_rtools=False).split(";"):
if rtools_string in p:
matches = re.search(rf"^(.*{rtools_string})", p)
if matches:
rtools_home = matches.group(1)
break
if not os.path.exists(rtools_home):
raise RuntimeError("Unable to find RTOOLS_HOME at: " + rtools_home)
print(f'Found Rtools at "{rtools_home}"', file=sys.stderr)
return rtools_home
def get_path_as_r(add_rtools: bool = True) -> str:
res = sp.check_output(
["Rscript", "-e", "Sys.getenv('PATH')"], stderr=sp.DEVNULL
).decode("utf-8")
matches = re.search(r"\"(.*)\"", res, re.MULTILINE)
if not matches:
return ""
path = [os.path.normpath(p) for p in matches.group(1).split(";")]
if add_rtools:
rtools_home = get_rtools_home()
path = [
os.path.join(rtools_home, "usr", "bin"),
os.path.join(rtools_home, "mingw64", "bin"),
] + path
return ";".join(path)
def r_which(program: str) -> str:
res = sp.check_output(
["Rscript", "-e", f'Sys.which("{program}")'], stderr=sp.DEVNULL
).decode("utf-8")
matches = re.search(r"\n\"(.*)\"", res, re.MULTILINE)
if not matches:
return ""
return os.path.normpath(matches.group(1))
def find_cc():
cc = r_which("gcc")
if cc == "":
cc = r_which("clang")
if cc == "":
raise RuntimeError("Unable to find a gcc or clang in your PATH")
return os.path.realpath(cc)
def find_cxx():
cxx = r_which("g++")
if cxx == "":
cxx = r_which("clang++")
if cxx == "":
raise RuntimeError("Unable to find a g++ or clang++ in your PATH")
return os.path.realpath(cxx)
def get_cc_version(cc) -> str:
cc_version = sp.check_output([cc, "-dumpversion"]).decode("ascii")
return re.search(r"^\d+", cc_version).group(0)
def get_cmake_version(env) -> str:
res = sp.check_output(["cmake", "--version"], env=env).decode("utf-8")
matches = re.search(r"(\d+\.\d+.\d+)", res)
if not matches:
raise RuntimeError("Unable to infer cmake version")
return matches.group(1)
def get_arch() -> str:
return platform.uname()[4].lower().replace("amd64", "x86_64")
def run_conan_profile_detect_windows(env):
assert os.name == "nt"
env["PATH"] = get_path_as_r()
arch = get_arch()
cc = find_cc()
cxx = find_cxx()
cc_version = get_cc_version(cc)
cmake_version = get_cmake_version(env)
env["CC"] = cc
env["CXX"] = cxx
# HDF5, szip and zlib come with Rtools, and using the version from Conan causes
# weird link errors that are difficult to address.
# So we claim that hdf4/1.14.3 is available as a system library (even though
# a different version is likely installed) and call it a day
profile = [
"[settings]",
f"arch={arch}",
"build_type=Release",
"compiler=gcc",
"compiler.cppstd=17",
f"compiler.version={cc_version}",
"os=Windows",
"[buildenv]",
f"PATH='" + env["PATH"] + "'",
"[platform_requires]",
"hdf5/1.14.3",
"[platform_tool_requires]",
f"cmake/{cmake_version}",
]
conan_profile = os.path.join(
get_conan_home(),
"profiles",
"default",
)
os.makedirs(os.path.dirname(conan_profile), exist_ok=True)
with open(conan_profile, "w") as f:
print("\n".join(profile), file=f, end="")
def run_conan_profile_detect(conan, env):
if os.name == "nt":
run_conan_profile_detect_windows(env)
return
sp.run([conan, "profile", "detect"], stdout=sp.DEVNULL, env=env)
conan_profile = os.path.join(
env.get("CONAN_HOME", os.path.join(os.path.expanduser("~"), ".conan2")),
"profiles",
"default",
)
with open(conan_profile) as f:
for line in f:
print(line, file=sys.stderr)
def run_conan_install(conan, env):
conanfile = os.path.join("..", "conanfile.txt")
sp.check_call(
[
conan,
"install",
conanfile,
"--settings=build_type=Release",
"--settings=compiler.cppstd=17",
"--output-folder=conan-staging",
"--build=missing",
"--update",
],
stdout=sp.DEVNULL,
env=env,
)
def main():
conan = find_conan()
pwd = os.getcwd()
env = os.environ.copy()
conandeps_mk = os.path.join(pwd, "conan-staging", "conandeps.mk")
if os.path.exists(conandeps_mk):
print(conandeps_mk)
return
conan_home = get_conan_home()
if conan_home is not None:
os.makedirs(conan_home, exist_ok=True)
run_conan_profile_detect(conan, env)
run_conan_install(conan, env)
print(conandeps_mk)
if __name__ == "__main__":
main()