Skip to content

Commit ef178ce

Browse files
Merge pull request #322 from Distributive-Network/caleb/build/overhaul
Improved Build System
2 parents 926313b + 47cb5be commit ef178ce

File tree

88 files changed

+6307
-4882
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+6307
-4882
lines changed

.git-blame-ignore-revs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# chore(linting): lint all python files
2+
aae30e864449442cf0b04e94f8a242b1b667de9a
3+
4+
# chore(linting): lint all JavaScript files
5+
16dc3153b3cb684ca72445ed058babc8f5d97f42
6+
7+
# chore(linting): lint all C++ files
8+
58cd4b45777b046f03a63255c1d93e289e1cab5e

.github/workflows/test-and-publish.yaml

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,38 @@ on:
99
workflow_call:
1010
workflow_dispatch:
1111
inputs:
12-
debug_enabled:
13-
type: boolean
14-
description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
12+
debug_enabled_os:
13+
type: choice
14+
description: Optionally, choose an OS to run the build with SSH debugging on (https://github.com/fawazahmed0/action-debug)
1515
required: false
16-
default: false
17-
dump_cores:
18-
type: boolean
19-
description: 'Include core dumps in CI artifacts'
16+
options:
17+
- ''
18+
- 'ubuntu-20.04'
19+
- 'macos-12'
20+
- 'macos-13'
21+
- 'macos-14'
22+
- 'windows-2019'
23+
debug_enabled_python:
24+
type: choice
25+
description: Choose a Python version to run the build with SSH debugging on
2026
required: false
21-
default: false
27+
options:
28+
- ''
29+
- '3.8'
30+
- '3.9'
31+
- '3.10'
32+
- '3.11'
33+
- '3.12'
34+
build_type:
35+
type: choice
36+
description: 'Choose the build type to use'
37+
required: false
38+
default: 'Debug'
39+
options:
40+
- 'Debug'
41+
- 'Profile'
42+
- 'DRelease'
43+
- 'Release'
2244
pull_request:
2345

2446
env:
@@ -100,11 +122,10 @@ jobs:
100122
fail-fast: false
101123
matrix:
102124
# The lowest supported version is Ubuntu 20.04 + Python 3.8 or macOS 12 + Python 3.9
103-
os: [ 'ubuntu-20.04', 'macos-12', 'windows-2019', 'macos-14' ]
125+
os: [ 'ubuntu-20.04', 'macos-12', 'macos-13', 'macos-14', 'windows-2019' ]
104126
python_version: [ '3.8', '3.9', '3.10', '3.11', '3.12' ]
105127
exclude:
106128
# macOS 12 comes with Python 3.9 by default, so we drop ci support for Python 3.8 on macOS
107-
# FIXME: We can't build on macOS 11 for now because our prebuilt `uncrustify` binary requires macOS 12
108129
- os: 'macos-12'
109130
python_version: '3.8'
110131
# actions/setup-python: The version '3.8'/'3.9' with architecture 'arm64' was not found for macOS.
@@ -165,7 +186,8 @@ jobs:
165186
- name: Build wheel
166187
run: |
167188
echo $(poetry run python --version)
168-
poetry build --format=wheel
189+
WORKFLOW_BUILD_TYPE=${{ inputs.build_type }}
190+
BUILD_TYPE=${WORKFLOW_BUILD_TYPE:-"Debug"} poetry build --format=wheel
169191
ls -lah ./dist/
170192
- name: Upload wheel as CI artifacts
171193
uses: actions/upload-artifact@v3
@@ -179,7 +201,7 @@ jobs:
179201
name: docs-${{ github.run_id }}-${{ github.sha }}
180202
path: ./build/docs/html/
181203
- name: Set cores to get stored in /cores
182-
if: ${{ matrix.os != 'windows-2019' && github.event_name == 'workflow_dispatch' && inputs.dump_cores }}
204+
if: ${{ matrix.os != 'windows-2019' }}
183205
# TODO (Caleb Aikens) figure out how to get Windows core dumps
184206
run: |
185207
sudo mkdir -p /cores
@@ -196,7 +218,8 @@ jobs:
196218
# TODO (Caleb Aikens) figure out how to get Windows core dumps
197219
ulimit -c unlimited
198220
fi
199-
poetry run python -m pip install --force-reinstall --verbose ./dist/*
221+
WORKFLOW_BUILD_TYPE=${{ inputs.build_type }}
222+
BUILD_TYPE=${WORKFLOW_BUILD_TYPE:-"Debug"} poetry run python -m pip install --force-reinstall --verbose ./dist/*
200223
poetry run python -m pytest tests/python
201224
- name: Run JS tests (peter-jr)
202225
if: ${{ (success() || failure()) }}
@@ -206,20 +229,18 @@ jobs:
206229
ulimit -c unlimited
207230
fi
208231
poetry run bash ./peter-jr ./tests/js/
232+
- name: SSH debug session
233+
if: ${{ (success() || failure()) && github.event_name == 'workflow_dispatch' && inputs.debug_enabled_os == matrix.os && inputs.debug_enabled_python == matrix.python_version}}
234+
uses: fawazahmed0/action-debug@main
235+
with:
236+
credentials: "admin:admin"
209237
- name: Upload core dumps as CI artifacts
210238
uses: actions/upload-artifact@v3
211-
if: ${{ matrix.os != 'windows-2019' && github.event_name == 'workflow_dispatch' && inputs.dump_cores }}
239+
if: ${{ matrix.os != 'windows-2019' && failure() }}
212240
# TODO (Caleb Aikens) figure out how to get Windows core dumps
213241
with:
214242
name: cores-${{ matrix.os }}-${{ matrix.python_version }}
215243
path: /cores
216-
# Enable tmate debugging of manually-triggered workflows if the input option was provided
217-
- name: SSH debug session
218-
if: ${{ (success() || failure()) && github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}
219-
uses: mxschmitt/action-tmate@v3
220-
with:
221-
detached: true
222-
limit-access-to-actor: true
223244
sdist:
224245
runs-on: ubuntu-20.04
225246
steps:
@@ -236,7 +257,7 @@ jobs:
236257
- name: Build source distribution (sdist) file
237258
run: |
238259
poetry self add "poetry-dynamic-versioning[plugin]"
239-
poetry build --format=sdist
260+
BUILD_DOCS=1 BUILD_TYPE=Release poetry build --format=sdist
240261
ls -lah ./dist/
241262
- name: Upload sdist as CI artifacts
242263
uses: actions/upload-artifact@v3

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ firefox-*/
1111
__pycache__
1212
Testing/Temporary
1313
_spidermonkey_install
14+
uncrustify-*.tar.gz
15+
uncrustify-*/
16+
uncrustify
17+
*.uncrustify
1418
__pycache__/*
1519
dist
1620
*.so

CMakeLists.txt

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,9 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
1717
set(CMAKE_CXX_EXTENSIONS OFF)
1818
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1919

20-
# Add an external; appends to `PYTHONMONKEY_EXTERNAL_FILES` in the parent scope.
21-
function(pythonmonkey_add_external PYTHONMONKEY_EXTERNAL)
22-
add_subdirectory("cmake/externals/${PYTHONMONKEY_EXTERNAL}")
23-
set(PYTHONMONKEY_EXTERNAL_FILE "cmake/externals/${PYTHONMONKEY_EXTERNAL}/CMakeLists.txt")
24-
source_group(
25-
TREE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/externals/${PYTHONMONKEY_EXTERNAL}"
26-
PREFIX "Externals\\${PYTHONMONKEY_EXTERNAL}"
27-
FILES "${PYTHONMONKEY_EXTERNAL_FILE}"
28-
)
29-
list(APPEND PYTHONMONKEY_EXTERNAL_FILES "${PYTHONMONKEY_EXTERNAL_FILE}")
30-
31-
set(PYTHONMONKEY_EXTERNAL_FILES ${PYTHONMONKEY_EXTERNAL_FILES} PARENT_SCOPE)
32-
endfunction()
33-
34-
file (GLOB SOURCE_FILES "src/*.cc" "src/internalBinding/*.cc") # Find all C++ files in the src directory
35-
file (GLOB HEADER_FILES "include/*.hh") # Find all header files in the include directory
36-
file (GLOB PYTHON_FILES "python/*.cc" "python/*.hh") # Find all the python bindings in the python directory
20+
file (GLOB_RECURSE HEADER_FILES "include/*.hh") # Find all header files in the include directory and below
21+
file (GLOB_RECURSE SOURCE_FILES "src/*.cc") # Find all C++ files in the src directory and below
22+
3723

3824
include_directories(${CMAKE_CURRENT_LIST_DIR})
3925

@@ -43,8 +29,50 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
4329
### Code block from: https://cliutils.gitlab.io/modern-cmake/chapters/projects/submodule.html
4430
include(FetchContent)
4531

46-
SET(COMPILE_FLAGS "-ggdb -Ofast -fno-rtti") # optimize but also emit debug symbols
47-
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS} $ENV{EXTRA_CMAKE_CXX_FLAGS}")
32+
if (WIN32)
33+
SET(COMPILE_FLAGS "/GR- /W0")
34+
35+
SET(OPTIMIZED "/O2")
36+
SET(UNOPTIMIZED "/Od")
37+
SET(KEEP_SYMBOLS "/DEBUG:FULL")
38+
SET(STRIP_SYMBOLS "/DEBUG:NONE")
39+
SET(PROFILE "/PROFILE")
40+
else()
41+
SET(COMPILE_FLAGS "-fno-rtti -Wno-invalid-offsetof")
42+
43+
SET(OPTIMIZED "-Ofast -DNDEBUG")
44+
SET(UNOPTIMIZED "-O0")
45+
SET(KEEP_SYMBOLS "-ggdb")
46+
SET(STRIP_SYMBOLS "-s")
47+
SET(PROFILE "-pg")
48+
endif()
49+
SET(PROFILE_FLAGS "${UNOPTIMIZED} ${KEEP_SYMBOLS} ${PROFILE}")
50+
SET(DEBUG_FLAGS "${UNOPTIMIZED} ${KEEP_SYMBOLS}")
51+
SET(DRELEASE_FLAGS "${OPTIMIZED} ${KEEP_SYMBOLS}")
52+
SET(RELEASE_FLAGS "${OPTIMIZED} ${STRIP_SYMBOLS}")
53+
54+
if(GENERATOR_IS_MULTI_CONFIG)
55+
set(CMAKE_CONFIGURATION_TYPES "Profile;Debug;DRelease;Release" CACHE STRING "" FORCE)
56+
string(APPEND COMPILE_FLAGS "$<$<CONFIG:Profile>:${PROFILE_FLAGS}> $<$<CONFIG:Debug>:${DEBUG_FLAGS}> $<$<CONFIG:DRelease>:${DRELEASE_FLAGS}> $<$<CONFIG:Release>:${RELEASE_FLAGS}>")
57+
else()
58+
set_property(CACHE PM_BUILD_TYPE PROPERTY HELPSTRING "Choose the type of build")
59+
set_property(CACHE PM_BUILD_TYPE PROPERTY STRINGS "Profile;Debug;DRelease;Release")
60+
if(PM_BUILD_TYPE STREQUAL "Profile")
61+
list(APPEND COMPILE_FLAGS "${PROFILE_FLAGS}")
62+
elseif(PM_BUILD_TYPE STREQUAL "Debug")
63+
list(APPEND COMPILE_FLAGS "${DEBUG_FLAGS}")
64+
elseif(PM_BUILD_TYPE STREQUAL "DRelease")
65+
list(APPEND COMPILE_FLAGS "${DRELEASE_FLAGS}")
66+
else() #Release build
67+
message("PM_BUILD_TYPE not detected or invalid value, defaulting to Release build.")
68+
set(PM_BUILD_TYPE Release CACHE STRING "" FORCE)
69+
list(APPEND COMPILE_FLAGS "${RELEASE_FLAGS}")
70+
endif()
71+
message("PythonMonkey build type is: ${PM_BUILD_TYPE}")
72+
list(JOIN COMPILE_FLAGS " " COMPILE_FLAGS)
73+
endif()
74+
75+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}")
4876

4977
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/modules)
5078
if(APPLE)
@@ -55,36 +83,35 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
5583
set(PYTHONLIBS_VERSION_STRING ${Python_VERSION})
5684
set(PYTHON_INCLUDE_DIR ${Python_INCLUDE_DIRS})
5785
set(PYTHON_LIBRARIES ${Python_LIBRARIES})
58-
message("Apple - Using Python:${Python_VERSION_MAJOR} - Libraries:${PYTHON_LIBRARIES} - IncludeDirs: ${PYTHON_INCLUDE_DIR}")
5986
elseif(UNIX)
6087
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
6188
set(Python_FIND_VIRTUALENV FIRST) # (require cmake >= v3.15 and this is the default) use the Python version configured by pyenv if available
6289
set(PYTHON_LIBRARIES ${Python_LIBRARIES})
6390
set(PYTHON_INCLUDE_DIR ${Python_INCLUDE_DIRS})
64-
message("Linux - Using Python:${Python_VERSION_MAJOR}.${Python_VERSION_MINOR} - Libraries:${PYTHON_LIBRARIES} - IncludeDirs: ${PYTHON_INCLUDE_DIR}")
6591
find_package(SpiderMonkey REQUIRED)
6692
elseif(WIN32)
67-
find_package(PythonInterp 3.8 REQUIRED)
68-
find_package(PythonLibs 3.8 REQUIRED)
93+
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)
94+
set(Python_FIND_VIRTUALENV FIRST) # (require cmake >= v3.15 and this is the default) use the Python version configured by pyenv if available
95+
set(PYTHON_LIBRARIES ${Python_LIBRARIES})
96+
set(PYTHON_INCLUDE_DIR ${Python_INCLUDE_DIRS})
6997
find_package(SpiderMonkey REQUIRED)
70-
set(PYTHONLIBS_VERSION_STRING $ENV{PY_VERSION})
7198
endif()
72-
include_directories(${PYTHON_INCLUDE_DIRS})
99+
message("${CMAKE_SYSTEM_NAME} - Using Python:${Python_VERSION} - Libraries:${Python_LIBRARIES} - IncludeDirs: ${Python_INCLUDE_DIRS}")
100+
include_directories(${Python_INCLUDE_DIRS})
73101
include_directories(${SPIDERMONKEY_INCLUDE_DIR})
74102

75103
# Add doxygen if this is the main app
76-
find_package(Doxygen)
77-
if(Doxygen_FOUND)
104+
option(BUILD_DOCS "Build documentation" OFF)
105+
if(BUILD_DOCS)
106+
find_package(Doxygen)
107+
if(Doxygen_FOUND)
78108
add_subdirectory(cmake/docs)
79-
else()
109+
else()
80110
message(STATUS "Doxygen not found. Not building docs.")
111+
endif()
81112
endif()
82113

83114
endif()
84115

85116
# Add compiled folder directories
86117
add_subdirectory(src)
87-
88-
pythonmonkey_add_external("uncrustify")
89-
pythonmonkey_add_external("autopep8")
90-
add_subdirectory(cmake/format)

Makefile

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,37 @@
44
# @date March 2024
55
#
66

7-
BUILD = debug
7+
BUILD = Debug # (case-insensitive) Release, DRelease, Debug, or Profile
8+
DOCS = false
9+
VERBOSE = true
810
PYTHON = python3
9-
RUN = poetry run
11+
RUN = poetry run
1012

11-
PYTHON_BUILD_ENV = VERBOSE=1 EXTRA_CMAKE_CXX_FLAGS="$(EXTRA_CMAKE_CXX_FLAGS)"
1213
OS_NAME := $(shell uname -s)
1314

1415
ifeq ($(OS_NAME),Linux)
15-
CPU_COUNT=$(shell cat /proc/cpuinfo | grep -c processor)
16-
MAX_JOBS=10
16+
CPU_COUNT = $(shell cat /proc/cpuinfo | grep -c processor)
17+
MAX_JOBS = 10
1718
CPUS := $(shell test $(CPU_COUNT) -lt $(MAX_JOBS) && echo $(CPU_COUNT) || echo $(MAX_JOBS))
1819
PYTHON_BUILD_ENV += CPUS=$(CPUS)
1920
endif
2021

21-
EXTRA_CMAKE_CXX_FLAGS = -Wno-invalid-offsetof $(JOBS)
22+
ifeq ($(BUILD),Profile)
23+
PYTHON_BUILD_ENV += BUILD_TYPE=Profile
24+
else ifeq ($(BUILD),Debug)
25+
PYTHON_BUILD_ENV += BUILD_TYPE=Debug
26+
else ifeq ($(BUILD),DRelease)
27+
PYTHON_BUILD_ENV += BUILD_TYPE=DRelease
28+
else # Release build
29+
PYTHON_BUILD_ENV += BUILD_TYPE=Release
30+
endif
31+
32+
ifeq ($(DOCS),true)
33+
PYTHON_BUILD_ENV += BUILD_DOCS=1
34+
endif
2235

23-
ifeq ($(BUILD),debug)
24-
EXTRA_CMAKE_CXX_FLAGS += -O0
36+
ifeq ($(VERBOSE),true)
37+
PYTHON_BUILD_ENV += VERBOSE=1
2538
endif
2639

2740
.PHONY: build test all clean debug
@@ -37,10 +50,9 @@ all: build test
3750
clean:
3851
rm -rf build/src/CMakeFiles/pythonmonkey.dir
3952
rm -f build/src/pythonmonkey.so
40-
rm -f python/pythonmonkey.so
53+
rm -f python/pythonmonkey/pythonmonkey.so
4154

4255
debug:
43-
@echo EXTRA_CMAKE_CXX_FLAGS=$(EXTRA_CMAKE_CXX_FLAGS)
4456
@echo JOBS=$(JOBS)
4557
@echo CPU_COUNT=$(CPU_COUNT)
46-
@echo OS_NAME=$(OS_NAME)
58+
@echo OS_NAME=$(OS_NAME)

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ Read this if you want to build a local version.
7878
- [Poetry](https://python-poetry.org/docs/#installation)
7979
- [poetry-dynamic-versioning](https://github.com/mtkennerly/poetry-dynamic-versioning)
8080

81-
2. Run `poetry install`. This command automatically compiles the project and installs the project as well as dependencies into the poetry virtualenv.
81+
2. Run `poetry install`. This command automatically compiles the project and installs the project as well as dependencies into the poetry virtualenv. If you would like to build the docs, set the `BUILD_DOCS` environment variable, like so: `BUILD_DOCS=1 poetry install`.
82+
PythonMonkey supports multiple build types, which you can build by setting the `BUILD_TYPE` environment variable, like so: `BUILD_TYPE=Debug poetry install`. The build types are (case-insensitive):
83+
- `Release`: stripped symbols, maximum optimizations (default)
84+
- `DRelease`: same as `Release`, except symbols are not stripped
85+
- `Debug`: minimal optimizations
86+
- `Profile`: same as `Debug`, except profiling is enabled
8287

8388
If you are using VSCode, you can just press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>B</kbd> to [run build task](https://code.visualstudio.com/docs/editor/tasks#_custom-tasks) - We have [the `tasks.json` file configured for you](.vscode/tasks.json).
8489

0 commit comments

Comments
 (0)