forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyPython.cmake
More file actions
78 lines (65 loc) · 2.55 KB
/
CopyPython.cmake
File metadata and controls
78 lines (65 loc) · 2.55 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
# Filename: CopyPython.cmake
#
# Description: When run, copies Python files from a source directory to a
# build directory located somewhere in the project's binary path. If
# PYTHON_EXECUTABLES is provided, it will also invoke compileall in the
# destination dir(s) to precache .pyc/.pyo files.
#
# Usage:
# This script is invoked via add_custom_target, like this:
# cmake -D OUTPUT_DIR="/out/dir/Release"
# -D SOURCE_DIR="/panda3d/direct/src/"
# -D PYTHON_EXECUTABLES="/usr/bin/python3.6"
# -P CopyPython.cmake
#
if(NOT CMAKE_SCRIPT_MODE_FILE)
message(FATAL_ERROR "CopyPython.cmake should not be included but run in script mode.")
return()
endif()
if(NOT DEFINED OUTPUT_DIR)
message(FATAL_ERROR "OUTPUT_DIR should be defined when running CopyPython.cmake!")
return()
endif()
# Ensure OUTPUT_DIR exists
file(MAKE_DIRECTORY ${OUTPUT_DIR})
# If there's a SOURCE_DIR, glob for .py files and copy
# (this is done by hand to avoid the CMake bug where it creates empty dirs)
if(DEFINED SOURCE_DIR)
file(GLOB_RECURSE py_files RELATIVE "${SOURCE_DIR}" "${SOURCE_DIR}/*.py")
foreach(py_file ${py_files})
get_filename_component(py_file_parent "${py_file}" DIRECTORY)
file(TIMESTAMP "${SOURCE_DIR}/${py_file}" src_stamp)
file(TIMESTAMP "${OUTPUT_DIR}/${py_file}" dst_stamp)
# The file is only copied if:
# - there's an __init__.py in its dir (or file is in the root) (i.e. file belongs to a package), and
# - the modification timestamp differs (i.e. file changed or never copied)
if((py_file_parent STREQUAL "." OR NOT py_file_parent
OR EXISTS "${SOURCE_DIR}/${py_file_parent}/__init__.py")
AND NOT src_stamp STREQUAL dst_stamp)
file(COPY "${SOURCE_DIR}/${py_file}" DESTINATION "${OUTPUT_DIR}/${py_file_parent}")
set(changed YES)
endif()
endforeach(py_file)
else()
# No SOURCE_DIR; assume we're outdated since our caller might be populating
# the OUTPUT_DIR themselves
set(changed YES)
endif()
# Make sure Python recognizes OUTPUT_DIR as a package
if(NOT EXISTS "${OUTPUT_DIR}/__init__.py")
file(WRITE "${OUTPUT_DIR}/__init__.py" "")
set(changed YES)
endif()
# Generate .pyc files for each Python version, if our caller wants
if(changed AND DEFINED PYTHON_EXECUTABLES)
foreach(interp ${PYTHON_EXECUTABLES})
execute_process(
COMMAND "${interp}" -m compileall .
OUTPUT_QUIET
WORKING_DIRECTORY "${OUTPUT_DIR}")
execute_process(
COMMAND "${interp}" -O -m compileall .
OUTPUT_QUIET
WORKING_DIRECTORY "${OUTPUT_DIR}")
endforeach(interp)
endif()