| 1 |
"""Base support for POSIX-like platforms.""" |
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
import py, os, sys |
| 5 |
|
| 6 |
from rpython.translator.platform import ( |
| 7 |
Platform, log, _run_subprocess, CompilationError) |
| 8 |
from rpython.config.support import detect_pax |
| 9 |
|
| 10 |
import rpython |
| 11 |
rpydir = str(py.path.local(rpython.__file__).join('..')) |
| 12 |
|
| 13 |
class BasePosix(Platform): |
| 14 |
exe_ext = '' |
| 15 |
make_cmd = 'make' |
| 16 |
so_prefixes = ('lib', '') |
| 17 |
|
| 18 |
relevant_environ = ('CPATH', 'LIBRARY_PATH', 'C_INCLUDE_PATH') |
| 19 |
|
| 20 |
DEFAULT_CC = 'gcc' |
| 21 |
rpath_flags = ['-Wl,-rpath=\'$$ORIGIN/\''] |
| 22 |
|
| 23 |
def __init__(self, cc=None): |
| 24 |
self.cc = cc or os.environ.get('CC', self.DEFAULT_CC) |
| 25 |
|
| 26 |
def _libs(self, libraries): |
| 27 |
return ['-l%s' % lib for lib in libraries] |
| 28 |
|
| 29 |
def _libdirs(self, library_dirs): |
| 30 |
assert '' not in library_dirs |
| 31 |
return ['-L%s' % ldir for ldir in library_dirs] |
| 32 |
|
| 33 |
def _includedirs(self, include_dirs): |
| 34 |
assert '' not in include_dirs |
| 35 |
return ['-I%s' % idir for idir in include_dirs] |
| 36 |
|
| 37 |
def _linkfiles(self, link_files): |
| 38 |
return list(link_files) |
| 39 |
|
| 40 |
def _compile_c_file(self, cc, cfile, compile_args): |
| 41 |
oname = self._make_o_file(cfile, ext='o') |
| 42 |
args = ['-c'] + compile_args + [str(cfile), '-o', str(oname)] |
| 43 |
self._execute_c_compiler(cc, args, oname, |
| 44 |
cwd=str(cfile.dirpath())) |
| 45 |
return oname |
| 46 |
|
| 47 |
def _link_args_from_eci(self, eci, standalone): |
| 48 |
return Platform._link_args_from_eci(self, eci, standalone) |
| 49 |
|
| 50 |
def _exportsymbols_link_flags(self): |
| 51 |
if (self.cc == 'mingw32' or (self.cc== 'gcc' and os.name=='nt') |
| 52 |
or sys.platform == 'cygwin'): |
| 53 |
return ["-Wl,--export-all-symbols"] |
| 54 |
return ["-Wl,--export-dynamic"] |
| 55 |
|
| 56 |
def _link(self, cc, ofiles, link_args, standalone, exe_name): |
| 57 |
args = [str(ofile) for ofile in ofiles] + link_args |
| 58 |
args += ['-o', str(exe_name)] |
| 59 |
if not standalone: |
| 60 |
args = self._args_for_shared(args, exe_name=exe_name) |
| 61 |
self._execute_c_compiler(cc, args, exe_name, |
| 62 |
cwd=str(exe_name.dirpath())) |
| 63 |
return exe_name |
| 64 |
|
| 65 |
def _pkg_config(self, lib, opt, default, check_result_dir=False): |
| 66 |
try: |
| 67 |
pkg_config = os.environ.get('PKG_CONFIG', 'pkg-config') |
| 68 |
ret, out, err = _run_subprocess(pkg_config, [lib, opt]) |
| 69 |
except OSError as e: |
| 70 |
err = str(e) |
| 71 |
ret = 1 |
| 72 |
if ret: |
| 73 |
result = default |
| 74 |
else: |
| 75 |
|
| 76 |
result = [entry[2:] for entry in out.split()] |
| 77 |
|
| 78 |
if not result: |
| 79 |
pass |
| 80 |
|
| 81 |
elif check_result_dir: |
| 82 |
|
| 83 |
for check in result: |
| 84 |
if os.path.isdir(check): |
| 85 |
break |
| 86 |
else: |
| 87 |
if ret: |
| 88 |
msg = ("running 'pkg-config %s %s' failed:\n%s\n" |
| 89 |
"and the default %r is not a valid directory" % ( |
| 90 |
lib, opt, err.rstrip(), default)) |
| 91 |
else: |
| 92 |
msg = ("'pkg-config %s %s' returned no valid directory:\n" |
| 93 |
"%s\n%s" % (lib, opt, out.rstrip(), err.rstrip())) |
| 94 |
raise ValueError(msg) |
| 95 |
return result |
| 96 |
|
| 97 |
def get_multiarch(self): |
| 98 |
if 'PYPY_MULTIARCH' in os.environ: |
| 99 |
return os.environ['PYPY_MULTIARCH'] |
| 100 |
if sys.platform == 'cygwin': |
| 101 |
return '' |
| 102 |
try: |
| 103 |
ret = self.execute(self.cc, args=['--print-multiarch']) |
| 104 |
except CompilationError: |
| 105 |
ret = '' |
| 106 |
else: |
| 107 |
ret = ret.out.strip() |
| 108 |
if not ret: |
| 109 |
|
| 110 |
|
| 111 |
from rpython.jit.backend import detect_cpu |
| 112 |
model = detect_cpu.autodetect() |
| 113 |
ret = model.replace('-', '_') + '-linux-gnu' |
| 114 |
if not ret: |
| 115 |
raise ValueError("cannot detect multiarch value on this platform") |
| 116 |
return ret |
| 117 |
|
| 118 |
def get_rpath_flags(self, rel_libdirs): |
| 119 |
|
| 120 |
return self.rpath_flags + ['-Wl,-rpath-link=\'%s\'' % ldir |
| 121 |
for ldir in rel_libdirs] |
| 122 |
|
| 123 |
def get_shared_only_compile_flags(self): |
| 124 |
return tuple(self.shared_only) + ('-fvisibility=hidden',) |
| 125 |
|
| 126 |
def makefile_link_flags(self): |
| 127 |
return list(self.link_flags) |
| 128 |
|
| 129 |
def gen_makefile(self, cfiles, eci, exe_name=None, path=None, |
| 130 |
shared=False, headers_to_precompile=[], |
| 131 |
no_precompile_cfiles = [], profopt=False, config=None): |
| 132 |
cfiles = self._all_cfiles(cfiles, eci) |
| 133 |
|
| 134 |
if path is None: |
| 135 |
path = cfiles[0].dirpath() |
| 136 |
|
| 137 |
rpypath = py.path.local(rpydir) |
| 138 |
|
| 139 |
if exe_name is None: |
| 140 |
exe_name = cfiles[0].new(ext=self.exe_ext) |
| 141 |
else: |
| 142 |
|
| 143 |
exe_name = exe_name + self.exe_ext |
| 144 |
|
| 145 |
linkflags = self.makefile_link_flags() |
| 146 |
m = GnuMakefile(path) |
| 147 |
if shared: |
| 148 |
linkflags = self._args_for_shared(linkflags) |
| 149 |
|
| 150 |
linkflags += self._exportsymbols_link_flags() |
| 151 |
|
| 152 |
if shared: |
| 153 |
libname = exe_name.basename |
| 154 |
target_name = 'lib' + exe_name.basename + '.' + self.so_ext |
| 155 |
m.so_name = path.join(target_name) |
| 156 |
else: |
| 157 |
target_name = exe_name.basename |
| 158 |
|
| 159 |
if shared: |
| 160 |
cflags = tuple(self.cflags) + self.get_shared_only_compile_flags() |
| 161 |
else: |
| 162 |
cflags = tuple(self.cflags) + tuple(self.standalone_only) |
| 163 |
|
| 164 |
|
| 165 |
if config and config.translation.lto: |
| 166 |
cflags = ('-flto',) + cflags |
| 167 |
|
| 168 |
m.exe_name = path.join(exe_name.basename) |
| 169 |
m.eci = eci |
| 170 |
|
| 171 |
def rpyrel(fpath): |
| 172 |
lpath = py.path.local(fpath) |
| 173 |
rel = lpath.relto(rpypath) |
| 174 |
if rel: |
| 175 |
return os.path.join('$(RPYDIR)', rel) |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
rel = lpath.relto(rpypath.join('..')) |
| 181 |
if rel: |
| 182 |
return os.path.join('$(RPYDIR)', '..', rel) |
| 183 |
m_dir = m.makefile_dir |
| 184 |
if m_dir == lpath: |
| 185 |
return '.' |
| 186 |
if m_dir.dirpath() == lpath: |
| 187 |
return '..' |
| 188 |
return fpath |
| 189 |
|
| 190 |
rel_cfiles = [m.pathrel(cfile) for cfile in cfiles] |
| 191 |
rel_ofiles = [rel_cfile[:rel_cfile.rfind('.')]+'.o' for rel_cfile in rel_cfiles] |
| 192 |
m.cfiles = rel_cfiles |
| 193 |
|
| 194 |
rel_includedirs = [rpyrel(incldir) for incldir in |
| 195 |
self.preprocess_include_dirs(eci.include_dirs)] |
| 196 |
rel_libdirs = [rpyrel(libdir) for libdir in |
| 197 |
self.preprocess_library_dirs(eci.library_dirs)] |
| 198 |
|
| 199 |
m.comment('automatically generated makefile') |
| 200 |
definitions = [ |
| 201 |
('RPYDIR', '"%s"' % rpydir), |
| 202 |
('TARGET', str(target_name)), |
| 203 |
('DEFAULT_TARGET', exe_name.basename), |
| 204 |
('SOURCES', rel_cfiles), |
| 205 |
('OBJECTS', rel_ofiles), |
| 206 |
('LIBS', self._libs(eci.libraries) + list(self.extra_libs)), |
| 207 |
('LIBDIRS', self._libdirs(rel_libdirs)), |
| 208 |
('INCLUDEDIRS', self._includedirs(rel_includedirs)), |
| 209 |
('CFLAGS', cflags), |
| 210 |
('CFLAGSEXTRA', list(eci.compile_extra)), |
| 211 |
('LDFLAGS', linkflags), |
| 212 |
('LDFLAGS_LINK', list(self.link_flags)), |
| 213 |
('LDFLAGSEXTRA', list(eci.link_extra)), |
| 214 |
('CC', self.cc), |
| 215 |
('CC_LINK', eci.use_cpp_linker and 'g++' or '$(CC)'), |
| 216 |
('LINKFILES', eci.link_files), |
| 217 |
('RPATH_FLAGS', self.get_rpath_flags(rel_libdirs)), |
| 218 |
] |
| 219 |
if profopt==True and shared==True: |
| 220 |
definitions.append(('PROFOPT_TARGET', exe_name.basename)) |
| 221 |
|
| 222 |
for args in definitions: |
| 223 |
m.definition(*args) |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
postcompile_rule = ('postcompile', '', ['true']) |
| 230 |
if detect_pax(): |
| 231 |
postcompile_rule[2].append('attr -q -s pax.flags -V m $(BIN)') |
| 232 |
|
| 233 |
if "gcc" in self.cc and headers_to_precompile: |
| 234 |
precompiled_header = headers_to_precompile[0].basename |
| 235 |
pch = "%s.gch" % precompiled_header |
| 236 |
extra_rules = [(pch, str(precompiled_header), '$(CC) $(CFLAGS) $(CFLAGSEXTRA) -o $@ -c $< $(INCLUDEDIRS)')] |
| 237 |
m.definition('PRECOMPILEDHEADERS', [pch]) |
| 238 |
o_dependency = '%.c ' + pch |
| 239 |
else: |
| 240 |
pch = "" |
| 241 |
extra_rules = [] |
| 242 |
o_dependency = '%.c' |
| 243 |
rules = [ |
| 244 |
('all', '$(DEFAULT_TARGET)', []), |
| 245 |
('$(TARGET)', '$(OBJECTS)', ['$(CC_LINK) $(LDFLAGSEXTRA) -o $@ $(OBJECTS) $(LIBDIRS) $(LIBS) $(LINKFILES) $(LDFLAGS)', '$(MAKE) postcompile BIN=$(TARGET)']), |
| 246 |
('%.o', o_dependency, '$(CC) $(CFLAGS) $(CFLAGSEXTRA) -o $@ -c $< $(INCLUDEDIRS)'), |
| 247 |
('%.o', '%.s', '$(CC) $(CFLAGS) $(CFLAGSEXTRA) -o $@ -c $< $(INCLUDEDIRS)'), |
| 248 |
('%.o', '%.cxx', '$(CXX) $(CFLAGS) $(CFLAGSEXTRA) -o $@ -c $< $(INCLUDEDIRS)'), |
| 249 |
] + extra_rules |
| 250 |
|
| 251 |
for rule in rules: |
| 252 |
m.rule(*rule) |
| 253 |
|
| 254 |
m.rule(*postcompile_rule) |
| 255 |
|
| 256 |
if shared: |
| 257 |
m.definition('SHARED_IMPORT_LIB', str(libname)), |
| 258 |
m.definition('PYPY_MAIN_FUNCTION', "pypy_main_startup") |
| 259 |
m.rule('main.c', '', |
| 260 |
'echo "' |
| 261 |
'int $(PYPY_MAIN_FUNCTION)(int, char*[]); ' |
| 262 |
'int main(int argc, char* argv[]) ' |
| 263 |
'{ return $(PYPY_MAIN_FUNCTION)(argc, argv); }" > $@') |
| 264 |
m.rule('$(DEFAULT_TARGET)', ['$(TARGET)', 'main.o'], |
| 265 |
['$(CC_LINK) $(LDFLAGS_LINK) main.o -L. -l$(SHARED_IMPORT_LIB) -o $@ $(RPATH_FLAGS)', '$(MAKE) postcompile BIN=$(DEFAULT_TARGET)']) |
| 266 |
|
| 267 |
return m |
| 268 |
|
| 269 |
def execute_makefile(self, path_to_makefile, extra_opts=[]): |
| 270 |
if isinstance(path_to_makefile, GnuMakefile): |
| 271 |
path = path_to_makefile.makefile_dir |
| 272 |
else: |
| 273 |
path = path_to_makefile |
| 274 |
log.execute('make %s in %s' % (" ".join(extra_opts), path)) |
| 275 |
returncode, stdout, stderr = _run_subprocess( |
| 276 |
self.make_cmd, ['-C', str(path)] + extra_opts) |
| 277 |
self._handle_error(returncode, stdout, stderr, path.join('make')) |
| 278 |
|
| 279 |
class Definition(object): |
| 280 |
def __init__(self, name, value): |
| 281 |
self.name = name |
| 282 |
self.value = value |
| 283 |
|
| 284 |
def write(self, f): |
| 285 |
def write_list(prefix, lst): |
| 286 |
lst = lst or [''] |
| 287 |
for i, fn in enumerate(lst): |
| 288 |
fn = fn.replace('\\', '\\\\') |
| 289 |
print(prefix, fn, end=" ", file=f) |
| 290 |
if i < len(lst)-1: |
| 291 |
print('\\', file=f) |
| 292 |
else: |
| 293 |
print(file=f) |
| 294 |
prefix = ' ' * len(prefix) |
| 295 |
name, value = self.name, self.value |
| 296 |
if isinstance(value, str): |
| 297 |
f.write('%s = %s\n' % (name, value.replace('\\', '\\\\'))) |
| 298 |
else: |
| 299 |
write_list('%s =' % (name,), value) |
| 300 |
f.write('\n') |
| 301 |
|
| 302 |
class Rule(object): |
| 303 |
def __init__(self, target, deps, body): |
| 304 |
self.target = target |
| 305 |
self.deps = deps |
| 306 |
self.body = body |
| 307 |
|
| 308 |
def write(self, f): |
| 309 |
target, deps, body = self.target, self.deps, self.body |
| 310 |
if isinstance(deps, str): |
| 311 |
dep_s = deps |
| 312 |
else: |
| 313 |
dep_s = ' '.join(deps) |
| 314 |
f.write('%s: %s\n' % (target, dep_s)) |
| 315 |
if isinstance(body, str): |
| 316 |
f.write('\t%s\n' % body) |
| 317 |
elif body: |
| 318 |
f.write('\t%s\n' % '\n\t'.join(body)) |
| 319 |
f.write('\n') |
| 320 |
|
| 321 |
class Comment(object): |
| 322 |
def __init__(self, body): |
| 323 |
self.body = body |
| 324 |
|
| 325 |
def write(self, f): |
| 326 |
f.write('# %s\n' % (self.body,)) |
| 327 |
|
| 328 |
class GnuMakefile(object): |
| 329 |
def __init__(self, path=None): |
| 330 |
self.defs = {} |
| 331 |
self.lines = [] |
| 332 |
self.makefile_dir = py.path.local(path) |
| 333 |
|
| 334 |
def pathrel(self, fpath): |
| 335 |
if fpath.dirpath() == self.makefile_dir: |
| 336 |
return fpath.basename |
| 337 |
elif fpath.dirpath().dirpath() == self.makefile_dir.dirpath(): |
| 338 |
assert fpath.relto(self.makefile_dir.dirpath()), ( |
| 339 |
"%r should be relative to %r" % ( |
| 340 |
fpath, self.makefile_dir.dirpath())) |
| 341 |
path = '../' + fpath.relto(self.makefile_dir.dirpath()) |
| 342 |
return path.replace('\\', '/') |
| 343 |
else: |
| 344 |
return str(fpath) |
| 345 |
|
| 346 |
def definition(self, name, value): |
| 347 |
defs = self.defs |
| 348 |
defn = Definition(name, value) |
| 349 |
if name in defs: |
| 350 |
self.lines[defs[name]] = defn |
| 351 |
else: |
| 352 |
defs[name] = len(self.lines) |
| 353 |
self.lines.append(defn) |
| 354 |
|
| 355 |
def rule(self, target, deps, body): |
| 356 |
self.lines.append(Rule(target, deps, body)) |
| 357 |
|
| 358 |
def comment(self, body): |
| 359 |
self.lines.append(Comment(body)) |
| 360 |
|
| 361 |
def write(self, out=None): |
| 362 |
if out is None: |
| 363 |
f = self.makefile_dir.join('Makefile').open('w') |
| 364 |
else: |
| 365 |
f = out |
| 366 |
for line in self.lines: |
| 367 |
line.write(f) |
| 368 |
f.flush() |
| 369 |
if out is None: |
| 370 |
f.close() |