From fe1738eb6e1c58b4994ffb72c4742864be87fbc0 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 4 Jun 2022 11:32:59 +0200 Subject: [PATCH 1/6] cmake: install parse_fasm.so Signed-off-by: Xiretza --- setup.py | 3 +-- src/CMakeLists.txt | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index cfbaa55..0a1b5a3 100644 --- a/setup.py +++ b/setup.py @@ -168,8 +168,7 @@ def build_extension(self, ext): os.path.dirname(self.get_ext_fullpath(ext.name))), ext.prefix) cmake_args = [ - '-DCMAKE_INSTALL_PREFIX=' + extdir, - '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir, + '-DCMAKE_INSTALL_PREFIX=' + extdir, '-DCMAKE_INSTALL_LIBDIR=.', '-DPYTHON_EXECUTABLE=' + sys.executable, '-DANTLR_RUNTIME_TYPE=' + shared_options.antlr_runtime ] diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 94783a6..2fdc164 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -97,6 +97,7 @@ add_library(parse_fasm SHARED ParseFasm.cpp ${ANTLR_FasmLexer_CXX_OUTPUTS} ${ANTLR_FasmParser_CXX_OUTPUTS}) target_link_libraries(parse_fasm ${ANTLR4_RUNTIME}) +install(TARGETS parse_fasm) #target_compile_options(parse_fasm PRIVATE -Wno-attributes) # Disable warning from antlr4-runtime add_executable(parse_fasm_tests From 9522bc49bedac337d79866bdf5dc0828da0d972c Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 4 Jun 2022 19:35:34 +0200 Subject: [PATCH 2/6] cmake: install tags.py properly Signed-off-by: Xiretza --- src/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2fdc164..596949d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -80,13 +80,15 @@ antlr_target(FasmParser antlr/FasmParser.g4 PARSER VISITOR # line 1: Skip lines starting in #define # 2: Extract TAGS(...) from dependencies # 3: Convert from TAGS('c', long_name) -> long_name = b'c, write to file -add_custom_target(tags.py ALL +add_custom_command(OUTPUT tags.py COMMAND grep -ve ^\#define ${CMAKE_CURRENT_SOURCE_DIR}/ParseFasm.cpp | grep -hoe TAG\([^\)]*\) | - sed -e s/TAG\(\\\(.*\\\),\ \\\(.*\\\)\)/\\2\ =\ b\\1/ > tags.py + sed -e s/TAG\(\\\(.*\\\),\ \\\(.*\\\)\)/\\2\ =\ b\\1/ > ${CMAKE_CURRENT_BINARY_DIR}/tags.py DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/ParseFasm.cpp VERBATIM ) +add_custom_target(tags_py ALL DEPENDS tags.py) +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tags.py TYPE LIB) # Include generated files in project environment include_directories(${ANTLR_FasmLexer_OUTPUT_DIR}) @@ -123,5 +125,3 @@ include(CTest) add_test(NAME parse_fasm_tests COMMAND parse_fasm_tests) enable_testing() - -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/tags.py DESTINATION .) From db3b3fc2241005329e02a549ada1a0f099bf070a Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 11 Jun 2022 17:50:59 +0200 Subject: [PATCH 3/6] fix(setup.py): compute install directory before outside build_extension This fixes builds with `setup.py develop`/`pip install -e`. build_ext.run() unsets build_ext.inplace before running build_extension(), and only restores it before calling copy_extensions_to_source(). Because the cmake extension has to know the real install directory in build_extension(), we need to compute it before calling build_ext.run() and then retrieve it in build_extension(). Signed-off-by: Xiretza --- setup.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 0a1b5a3..3c783ae 100644 --- a/setup.py +++ b/setup.py @@ -108,6 +108,19 @@ def copy_extensions_to_source(self): def run(self): shared_options.load(self) try: + # We need to compute the install directory here, because + # build_ext.run() unsets build_ext.inplace while running + # build_extension(). This means that files would be installed + # to the wrong location (to the temporary build directory) + # for `setup.py develop`/`pip install -e` builds. + self.install_dirs = {} + for ext in self.extensions: + if isinstance(ext, CMakeExtension): + self.install_dirs[ext] = os.path.join( + os.path.abspath( + os.path.dirname(self.get_ext_fullpath(ext.name))), + ext.prefix) + super().run() try: @@ -163,10 +176,7 @@ def add_flags(self): def build_extension(self, ext): if isinstance(ext, CMakeExtension): - extdir = os.path.join( - os.path.abspath( - os.path.dirname(self.get_ext_fullpath(ext.name))), - ext.prefix) + extdir = self.install_dirs[ext] cmake_args = [ '-DCMAKE_INSTALL_PREFIX=' + extdir, '-DCMAKE_INSTALL_LIBDIR=.', '-DPYTHON_EXECUTABLE=' + sys.executable, From 7465af260ab4b09f95372e239120a8d664ff1c9c Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 4 Jun 2022 20:42:09 +0200 Subject: [PATCH 4/6] setup.py: don't build everything twice super().run() already builds all the extensions Signed-off-by: Xiretza --- setup.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 3c783ae..51b6dbb 100644 --- a/setup.py +++ b/setup.py @@ -121,8 +121,6 @@ def run(self): os.path.dirname(self.get_ext_fullpath(ext.name))), ext.prefix) - super().run() - try: out = subprocess.check_output(['cmake', '--version']) except OSError: @@ -136,8 +134,7 @@ def run(self): if cmake_version < '3.7.0': raise RuntimeError("CMake >= 3.7.0 is required.") - for ext in self.extensions: - self.build_extension(ext) + super().run() except BaseException as e: print( From 73d1d20595fc8a6915edcee77adb000790f345ec Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 4 Jun 2022 20:46:44 +0200 Subject: [PATCH 5/6] cmake: allow overriding ANTLR_EXECUTABLE Signed-off-by: Xiretza --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 596949d..92d7938 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,7 +57,7 @@ include_directories(${ANTLR4_INCLUDE_DIRS}) # set variable pointing to the antlr tool that supports C++ # this is not required if the jar file can be found under PATH environment -set(ANTLR_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/antlr4_lib/antlr-4.9.3-complete.jar) +set(ANTLR_EXECUTABLE ${CMAKE_CURRENT_SOURCE_DIR}/../third_party/antlr4_lib/antlr-4.9.3-complete.jar CACHE PATH "antlr4 generator JAR file") # add macros to generate ANTLR Cpp code from grammar find_package(ANTLR REQUIRED) From 9ebb73009ab9438d670fde2f441dcbff1047af1c Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 4 Jun 2022 20:48:31 +0200 Subject: [PATCH 6/6] cmake: explicitly link test with gtest With external (distro) gtest, this avoids an error along the lines of "DSO missing from command line". Signed-off-by: Xiretza --- src/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 92d7938..f407785 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -108,6 +108,7 @@ add_executable(parse_fasm_tests ${ANTLR_FasmParser_CXX_OUTPUTS}) target_link_libraries(parse_fasm_tests ${ANTLR4_RUNTIME}) target_link_libraries(parse_fasm_tests gtest_main) +target_link_libraries(parse_fasm_tests gtest) #target_compile_options(parse_fasm_tests PRIVATE -Wno-attributes) # Disable warning from antlr4-runtime # Standalone executable