forked from rock-simulation/pybob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.py
More file actions
189 lines (170 loc) · 8.28 KB
/
environment.py
File metadata and controls
189 lines (170 loc) · 8.28 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
#! /usr/bin/env python
import os
import sys
from platform import system
import colorconsole as c
import subprocess
import execute
QT5_UBUNTU = False
if system() == "Linux":
qt5_ubuntu_env_var = os.environ.get("QT5_UBUNTU")
if qt5_ubuntu_env_var is None:
try:
import distro
except ImportError:
raise ImportError("please install the distro package: pip install distro\n"
"(or set QT5_UBUNTU to True or False prior to using pybob)")
QT5_UBUNTU = distro.id() == "ubuntu" and int(distro.major_version(best=True)) >= 20
else:
QT5_UBUNTU = bool(qt5_ubuntu_env_var)
def source(sourceFile):
newenv = {}
cmd = ["echo", "'source", sourceFile, "&>", "/dev/null", ";", "env'", "|", "bash"]
cmdString = " ".join(cmd)
p = subprocess.Popen(cmdString, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
for line in out.split(b"\n"):
try:
k,v = line.strip().split('=',1)
except:
continue # bad line format, skip it
newenv[k] = v
os.environ.update(newenv)
def setupEnv(cfg, update=False):
global os
prefix = cfg["devDir"] + "/install"
if system() == "Windows":
if prefix[1] == ':':
prefix = prefix.replace(prefix[:2], "/"+prefix[0])
prefix_bin = prefix + "/bin"
prefix_lib = prefix + "/lib"
prefix_pkg = prefix_lib + "/pkgconfig"
pythonver = "%d.%d" % (sys.version_info.major, sys.version_info.minor)
pythonpath = prefix_lib + "/python%d.%d/site-packages" % (sys.version_info.major, sys.version_info.minor)
platform = system()
if platform == "Windows":
# todo: make this more generic
pythonpath = "/mingw64/lib/python"+pythonver+":/mingw64/lib/python"+pythonver+"/plat-win32:/mingw64/lib/python"+pythonver+"/lib-tk:/mingw64/lib/python"+pythonver+"/lib-dynload:/mingw64/lib/python"+pythonver+"/site-packages:"+pythonpath
elif platform == "Linux":
prefix_lib += ":" + prefix + "/lib/x86_64-linux-gnu"
prefix_pkg += ":" + prefix + "/lib/x86_64-linux-gnu/pkgconfig"
prefix_config = prefix + "/configuration"
# create env.sh
p = subprocess.Popen("which cmake_debug", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
cmakeDebugPath = out.strip()
if len(cmakeDebugPath) > 0:
# check if path is correct
expectPath = cfg["devDir"]+"/install/bin/cmake_debug"
if platform == "Windows":
c.printWarning("cmake_debug path check is not working on Windows currently (please always ensure that you only sourced the env.sh in your current dev folder!")
else :
if cmakeDebugPath.decode("utf-8") != expectPath:
c.printError('"cmake_debug" found in wrong folder.')
c.printError('Found: ' + cmakeDebugPath.decode("utf-8"))
c.printError('Expected: ' + expectPath)
c.printError('Maybe you already sourced an "env.sh" from a different "dev" folder?')
return
if not update:
return
if not update:
if os.path.isfile(cfg["devDir"]+"/env.sh"):
source(cfg["devDir"]+"/env.sh")
p = subprocess.Popen("which autoproj", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, err = p.communicate()
aPath = out.strip()
if len(aPath) > 0:
with open(cfg["devDir"]+"/bobenv.sh", "w") as f:
f.write("#! /bin/bash\n")
f.write(". env.sh\n")
f.write('export MARS_SCRIPT_DIR="'+cfg["pyScriptDir"]+'"\n')
_make_pybob_aliases(f)
else:
with open(cfg["devDir"]+"/env.sh", "w") as f:
f.write("#! /bin/bash\n")
f.write('export AUTOPROJ_CURRENT_ROOT="'+cfg["devDir"]+'"\n')
f.write('if [ x${CMAKE_PREFIX_PATH} = "x" ]; then\n')
f.write(' export CMAKE_PREFIX_PATH="'+cfg["devDir"]+'/install"\n')
f.write('else\n')
f.write(' export CMAKE_PREFIX_PATH="'+cfg["devDir"]+'/install:$CMAKE_PREFIX_PATH"\n')
f.write('fi\n')
f.write('export MARS_SCRIPT_DIR="'+cfg["pyScriptDir"]+'"\n')
f.write('export PATH="$PATH:'+prefix_bin+'"\n')
if platform == "Darwin":
f.write('export DYLD_LIBRARY_PATH="'+prefix_lib+':$DYLD_LIBRARY_PATH"\n')
f.write('export MYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH"\n')
f.write('export USE_QT5=1\n')
elif platform == "Linux":
f.write('export LD_LIBRARY_PATH="'+prefix_lib+':$LD_LIBRARY_PATH"\n')
f.write('export CXXFLAGS="-std=c++11"\n')
if QT5_UBUNTU:
f.write('export USE_QT5=1\n')
else:
f.write('export PATH="'+prefix_lib+':$PATH"\n')
f.write('export USE_QT5=1\n')
f.write('export ROCK_CONFIGURATION_PATH="'+prefix_config+'"\n')
f.write('export PYTHONPATH="' + pythonpath + ':$PYTHONPATH"\n')
f.write('if [ x${PKG_CONFIG_PATH} = "x" ]; then\n')
f.write(' export PKG_CONFIG_PATH="'+prefix_pkg+'"\n')
f.write('else\n')
f.write(' export PKG_CONFIG_PATH="'+prefix_pkg+':$PKG_CONFIG_PATH"\n')
f.write('fi\n')
_make_pybob_aliases(f)
execute.makeDir(cfg["devDir"]+"/install/bin")
if len(aPath) == 0:
with open(cfg["devDir"]+"/install/bin/amake", "w") as f:
f.write("#!/bin/bash\n")
f.write("${AUTOPROJ_CURRENT_ROOT}/pybob/pybob.py install $@\n")
cmd = ["chmod", "+x", cfg["devDir"]+"/install/bin/amake"]
execute.simpleExecute(cmd)
with open(cfg["devDir"]+"/install/bin/cmake_debug", "w") as f:
f.write("#!/bin/bash\n")
options = "-DROCK_TEST_ENABLED=OFF"
if not "autoprojEnv" in cfg or not cfg["autoprojEnv"]:
options += " -DBINDINGS_RUBY=OFF "
if platform == "Windows":
f.write("cmake .. "+options+"-DCMAKE_INSTALL_PREFIX=$AUTOPROJ_CURRENT_ROOT/install -DCMAKE_BUILD_TYPE=Debug -G \"MSYS Makefiles\" $@\n")
else:
f.write("cmake .. "+options+"-DCMAKE_INSTALL_PREFIX=$AUTOPROJ_CURRENT_ROOT/install -DCMAKE_BUILD_TYPE=Debug $@\n")
with open(cfg["devDir"]+"/install/bin/cmake_release", "w") as f:
f.write("#!/bin/bash\n")
if platform == "Windows":
f.write("cmake .. "+options+"-DCMAKE_INSTALL_PREFIX=$AUTOPROJ_CURRENT_ROOT/install -DCMAKE_BUILD_TYPE=Release -G \"MSYS Makefiles\" $@\n")
else:
f.write("cmake .. "+options+"-DCMAKE_INSTALL_PREFIX=$AUTOPROJ_CURRENT_ROOT/install -DCMAKE_BUILD_TYPE=Release $@\n")
cmd = ["chmod", "+x", cfg["devDir"]+"/install/bin/cmake_debug"]
execute.simpleExecute(cmd)
cmd = ["chmod", "+x", cfg["devDir"]+"/install/bin/cmake_release"]
execute.simpleExecute(cmd)
source(cfg["devDir"]+"/env.sh")
def _make_pybob_aliases(f):
env_variables = []
if "PYTHON" in os.environ:
PYTHON_ALIAS_PREFIX = "$PYTHON "
else:
PYTHON_ALIAS_PREFIX = ""
f.write("alias bob='%s${MARS_SCRIPT_DIR}/pybob.py'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-bootstrap='%s${MARS_SCRIPT_DIR}/pybob.py bootstrap'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-install='%s${MARS_SCRIPT_DIR}/pybob.py install'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-rebuild='%s${MARS_SCRIPT_DIR}/pybob.py rebuild'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-build='%s${MARS_SCRIPT_DIR}/pybob.py'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-diff='%s${MARS_SCRIPT_DIR}/pybob.py diff'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-list='%s${MARS_SCRIPT_DIR}/pybob.py list'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-fetch='%s${MARS_SCRIPT_DIR}/pybob.py fetch'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-show-log='%s${MARS_SCRIPT_DIR}/pybob.py show-log'\n"
% PYTHON_ALIAS_PREFIX)
f.write("alias bob-envsh='%s${MARS_SCRIPT_DIR}/pybob.py envsh'\n"
% PYTHON_ALIAS_PREFIX)
f.write(". ${MARS_SCRIPT_DIR}/auto_complete.sh\n")
if "PYTHON" in os.environ:
f.write("export PYTHON=%s\n" % os.environ["PYTHON"])
if "CYTHON" in os.environ:
f.write("export CYTHON=%s\n" % os.environ["CYTHON"])