Skip to content
Permalink
Browse files

buildsystem: move `run.py` to build directory

- disallow running `run.py.in` directly
  • Loading branch information
tusharpm committed Feb 26, 2019
1 parent a644b10 commit 7c04cd4832a68b747792d0730bc054d4864a74cb
Showing with 47 additions and 38 deletions.
  1. +3 −2 CMakeLists.txt
  2. +28 −24 buildsystem/cythonize.py
  3. +7 −5 buildsystem/pxdgen.py
  4. +5 −6 buildsystem/python.cmake
  5. +4 −1 run.py → run.py.in
@@ -216,8 +216,9 @@ target_compile_options(pyext_libopenage INTERFACE
target_link_libraries(pyext_libopenage INTERFACE libopenage)
set(PYEXT_LINK_LIBRARY pyext_libopenage)

add_cython_modules(EMBED NOINSTALL run.py)
add_py_modules(BININSTALL run.py AS openage)
configure_file(run.py.in run.py)
add_cython_modules(EMBED NOINSTALL ${CMAKE_CURRENT_BINARY_DIR}/run.py)
add_py_modules(BININSTALL ${CMAKE_CURRENT_BINARY_DIR}/run.py AS openage)
add_subdirectory(openage/)

python_finalize()
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# Copyright 2015-2018 the openage authors. See copying.md for legal info.
# Copyright 2015-2019 the openage authors. See copying.md for legal info.

"""
Runs Cython on all modules that were listed via add_cython_module.
@@ -11,6 +11,7 @@
import sys
from contextlib import redirect_stdout
from multiprocessing import cpu_count
from pathlib import Path

from Cython.Build import cythonize

@@ -51,7 +52,7 @@ class CythonFilter(LineFilter):
# pylint: disable=too-few-public-methods
def __init__(self):
filters = [
lambda x: x == 'Please put "# distutils: language=c++" in your .pyx or .pxd file(s)',
lambda x: 'put "# distutils: language=c++" in your .pyx or .pxd file(s)' in x,
lambda x: x.startswith('Compiling ') and x.endswith(' because it changed.')
]
super().__init__(filters=filters)
@@ -62,34 +63,36 @@ def read_list_from_file(filename):
with open(filename) as fileobj:
data = fileobj.read().strip()

data = [os.path.realpath(os.path.normpath(filename)) for filename in data.split(';')]
if data == ['']:
return []

return data


def convert_to_relpath(filenames):
""" Convert a list of absolute paths to relative paths """
cwd = os.getcwd()
return [os.path.relpath(filename, cwd) for filename in filenames]
return [Path(filename).resolve() for filename in data.split(';')]


def remove_if_exists(filename):
""" Deletes the file (if it exists) """
if os.path.exists(filename):
print(os.path.relpath(filename, os.getcwd()))
os.remove(filename)
if filename.is_file():
print(filename.relative_to(os.getcwd()))
filename.unlink()


def cythonize_wrapper(modules, **kwargs):
""" Calls cythonize, filtering useless warnings """
if not modules:
return
bin_dir, bin_modules = kwargs['build_dir'], []
src_dir, src_modules = Path.cwd(), []

for module in modules:
if Path(bin_dir) in module.parents:
bin_modules.append(str(module.relative_to(bin_dir)))
else:
src_modules.append(str(module.relative_to(src_dir)))

with CythonFilter() as cython_filter:
with redirect_stdout(cython_filter):
cythonize(modules, **kwargs)
if src_modules:
cythonize(src_modules, **kwargs)

if bin_modules:
os.chdir(bin_dir)
cythonize(bin_modules, **kwargs)
os.chdir(src_dir)


def main():
@@ -126,15 +129,16 @@ def main():

# cython emits warnings on using absolute paths to modules
# https://github.com/cython/cython/issues/2323
modules = convert_to_relpath(read_list_from_file(args.module_list))
embedded_modules = convert_to_relpath(read_list_from_file(args.embedded_module_list))
modules = read_list_from_file(args.module_list)
embedded_modules = read_list_from_file(args.embedded_module_list)
depends = set(read_list_from_file(args.depends_list))

if args.clean:
for module in modules + embedded_modules:
module = os.path.splitext(module)[0]
remove_if_exists(module + '.cpp')
remove_if_exists(module + '.html')
rel_module = module.relative_to(Path.cwd())
build_module = args.build_dir / rel_module
remove_if_exists(build_module.with_suffix('.cpp'))
remove_if_exists(build_module.with_suffix('.html'))
sys.exit(0)

from Cython.Compiler import Options
@@ -427,7 +427,9 @@ def parse_args():
def main():
""" CLI entry point """
args = parse_args()
cppdir = Path("libopenage").absolute()
cppname = "libopenage"
cppdir = Path(cppname).absolute()
out_cppdir = Path(args.output_dir) / cppname

if args.verbose:
hdr_count = len(args.all_files)
@@ -442,9 +444,9 @@ def main():
print("pxdgen source file is not in " + cppdir + ": " + filename)
sys.exit(1)

# join args.output_dir with relative path from CWD
pxdfile_relpath = filename.with_suffix('.pxd').relative_to(CWD)
pxdfile = args.output_dir / pxdfile_relpath
# join out_cppdir with relative path from cppdir
pxdfile_relpath = filename.with_suffix('.pxd').relative_to(cppdir)
pxdfile = out_cppdir / pxdfile_relpath

if args.verbose:
print("creating '{}' for '{}':".format(pxdfile, filename))
@@ -463,7 +465,7 @@ def main():
# create empty __init__.py in all parent directories.
# Cython requires this; else it won't find the .pxd files.
for dirname in pxdfile_relpath.parents:
template = args.output_dir / dirname / "__init__"
template = out_cppdir / dirname / "__init__"
for extension in ("py", "pxd"):
initfile = template.with_suffix("." + extension)
if not initfile.exists():
@@ -87,14 +87,13 @@ function(add_cython_modules)
set_source_files_properties("${CPPNAME}" PROPERTIES GENERATED ON)

# construct some hopefully unique target name
file(RELATIVE_PATH TARGETNAME "${CMAKE_SOURCE_DIR}" "${source}")
string(REGEX REPLACE "\\.pyx?$" "" TARGETNAME "${TARGETNAME}")
string(REGEX REPLACE "/" "_" TARGETNAME "${TARGETNAME}")
set(TARGETNAME "${REL_CURRENT_SOURCE_DIR}/${OUTPUTNAME}")
string(REPLACE "/" "_" TARGETNAME "${TARGETNAME}")

# generate the pretty module name
file(RELATIVE_PATH PRETTY_MODULE_NAME "${CMAKE_SOURCE_DIR}" "${source}")
string(REGEX REPLACE "\\.pyx?$" "" PRETTY_MODULE_NAME "${PRETTY_MODULE_NAME}")
string(REGEX REPLACE "/" "." PRETTY_MODULE_NAME "${PRETTY_MODULE_NAME}")
set(PRETTY_MODULE_NAME "${REL_CURRENT_SOURCE_DIR}/${OUTPUTNAME}")
string(REPLACE "/" "." PRETTY_MODULE_NAME "${PRETTY_MODULE_NAME}")
string(REGEX REPLACE "^\\." "" PRETTY_MODULE_NAME "${PRETTY_MODULE_NAME}")
set(PRETTY_MODULE_PROPERTIES "")

if(EMBED_NEXT)
@@ -10,6 +10,9 @@
which satisifies that requirement.
"""

if __name__ == '__main__':
if __name__ == '__main__@SOME_UNDEFINED_VARIABLE_CMAKE_WILL_REMOVE@':
from openage.__main__ import main
main()
else:
print("Running this in the source directory is not supported.",
"Please use `make run` or `bin/run.py` to start instead.")

0 comments on commit 7c04cd4

Please sign in to comment.
You can’t perform that action at this time.