Skip to content

Commit

Permalink
buildsystem: move run.py to build directory
Browse files Browse the repository at this point in the history
- disallow running `run.py.in` directly
  • Loading branch information
tusharpm committed Feb 27, 2019
1 parent a644b10 commit 7c04cd4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Expand Up @@ -216,8 +216,9 @@ target_compile_options(pyext_libopenage INTERFACE
target_link_libraries(pyext_libopenage INTERFACE libopenage) target_link_libraries(pyext_libopenage INTERFACE libopenage)
set(PYEXT_LINK_LIBRARY pyext_libopenage) set(PYEXT_LINK_LIBRARY pyext_libopenage)


add_cython_modules(EMBED NOINSTALL run.py) configure_file(run.py.in run.py)
add_py_modules(BININSTALL run.py AS openage) 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/) add_subdirectory(openage/)


python_finalize() python_finalize()
Expand Down
52 changes: 28 additions & 24 deletions buildsystem/cythonize.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/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. Runs Cython on all modules that were listed via add_cython_module.
Expand All @@ -11,6 +11,7 @@
import sys import sys
from contextlib import redirect_stdout from contextlib import redirect_stdout
from multiprocessing import cpu_count from multiprocessing import cpu_count
from pathlib import Path


from Cython.Build import cythonize from Cython.Build import cythonize


Expand Down Expand Up @@ -51,7 +52,7 @@ class CythonFilter(LineFilter):
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
def __init__(self): def __init__(self):
filters = [ 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.') lambda x: x.startswith('Compiling ') and x.endswith(' because it changed.')
] ]
super().__init__(filters=filters) super().__init__(filters=filters)
Expand All @@ -62,34 +63,36 @@ def read_list_from_file(filename):
with open(filename) as fileobj: with open(filename) as fileobj:
data = fileobj.read().strip() data = fileobj.read().strip()


data = [os.path.realpath(os.path.normpath(filename)) for filename in data.split(';')] return [Path(filename).resolve() 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]




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




def cythonize_wrapper(modules, **kwargs): def cythonize_wrapper(modules, **kwargs):
""" Calls cythonize, filtering useless warnings """ """ Calls cythonize, filtering useless warnings """
if not modules: bin_dir, bin_modules = kwargs['build_dir'], []
return 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 CythonFilter() as cython_filter:
with redirect_stdout(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(): def main():
Expand Down Expand Up @@ -126,15 +129,16 @@ def main():


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


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


from Cython.Compiler import Options from Cython.Compiler import Options
Expand Down
12 changes: 7 additions & 5 deletions buildsystem/pxdgen.py
Expand Up @@ -427,7 +427,9 @@ def parse_args():
def main(): def main():
""" CLI entry point """ """ CLI entry point """
args = parse_args() args = parse_args()
cppdir = Path("libopenage").absolute() cppname = "libopenage"
cppdir = Path(cppname).absolute()
out_cppdir = Path(args.output_dir) / cppname


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


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


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


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


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


if(EMBED_NEXT) if(EMBED_NEXT)
Expand Down
5 changes: 4 additions & 1 deletion run.py → run.py.in
Expand Up @@ -10,6 +10,9 @@
which satisifies that requirement. which satisifies that requirement.
""" """


if __name__ == '__main__': if __name__ == '__main__@SOME_UNDEFINED_VARIABLE_CMAKE_WILL_REMOVE@':
from openage.__main__ import main from openage.__main__ import main
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.