Skip to content

Commit

Permalink
Fix Python module (see issue #135) and make it depend on the existing…
Browse files Browse the repository at this point in the history
… shared library instead of rebuilding; Consolidate source file list
  • Loading branch information
TinoDidriksen committed Dec 27, 2023
1 parent 2e6d34a commit 83f415f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 86 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ jobs:
run: ./test/runall.pl
- name: install
run: sudo cmake --install .
- name: test python
run: |
python3 -c "import constraint_grammar; print(dir(constraint_grammar))"
python3 -c "import cg3; print(dir(cg3))"
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ configure_file(setup.py.in setup.py)

add_custom_command(OUTPUT ${CPP_WRAP_FILE} ${PYTHON_FILE}
COMMAND ${PYTHON_EXECUTABLE} setup.py build
DEPENDS cg3
COMMENT "Building ${PYTHON_FILE}"
)

Expand Down
45 changes: 13 additions & 32 deletions python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,33 @@
"""
Setup for SWIG Python bindings for constraint_grammar
"""
from os import path
from distutils.core import Extension, setup
from distutils.command.build import build
from sys import platform


class CustomBuild(build):
sub_commands = [
('build_ext', build.has_ext_modules),
('build_py', build.has_pure_modules),
('build_clib', build.has_c_libraries),
('build_scripts', build.has_scripts),
]


def get_sources():
sources = ['constraint_grammar.i', '${CMAKE_SOURCE_DIR}/include/posix/popen_plus.cpp']
cc_sources = [
'ApertiumApplicator.cpp', 'BinaryGrammar.cpp', 'BinaryGrammar_read.cpp', 'BinaryGrammar_read_10043.cpp',
'BinaryGrammar_write.cpp', 'Cohort.cpp', 'CohortIterator.cpp', 'ContextualTest.cpp', 'Grammar.cpp',
'GrammarApplicator.cpp', 'GrammarApplicator_matchSet.cpp', 'GrammarApplicator_reflow.cpp',
'GrammarApplicator_runContextualTest.cpp', 'GrammarApplicator_runGrammar.cpp',
'GrammarApplicator_runRules.cpp', 'GrammarWriter.cpp', 'MatxinApplicator.cpp',
'Reading.cpp', 'Rule.cpp', 'Set.cpp', 'SingleWindow.cpp', 'Strings.cpp', 'Tag.cpp',
'TextualParser.cpp', 'Window.cpp', 'uextras.cpp',
]
rel_path = '${CMAKE_SOURCE_DIR}/src/'
sources.extend(path.join(rel_path, f) for f in cc_sources)
return sources
compile_args = '${BUILD_DEFS} ${CMAKE_CXX_FLAGS}'.split()
link_args = []
if platform == 'darwin':
compile_args += ['-stdlib=libc++', '-mmacosx-version-min=10.10']
link_args.append('-mmacosx-version-min=10.10')

constraint_grammar_module = Extension(
name='_constraint_grammar',
sources=get_sources(),
sources=['constraint_grammar.i'],
swig_opts = ['-c++', '-I/usr/include', '-I${CMAKE_SOURCE_DIR}', '-I${CMAKE_SOURCE_DIR}/src', '-I${CMAKE_SOURCE_DIR}/include', '-Wall'],
include_dirs=['${CMAKE_SOURCE_DIR}', '${CMAKE_SOURCE_DIR}/src', '${CMAKE_SOURCE_DIR}/include', '${CMAKE_SOURCE_DIR}/include/posix'],
extra_compile_args='${BUILD_DEFS} ${CMAKE_CXX_FLAGS}'.split(),
extra_link_args=['-licuuc', '-licuio', '-licui18n'],
library_dirs=['${CMAKE_BINARY_DIR}/src'],
libraries=['cg3', 'icuuc', 'icuio', 'icui18n'],
extra_compile_args=compile_args,
extra_link_args=link_args,
)

setup(
name='constraint_grammar',
version='${VERSION}',
description='SWIG interface to constraint_grammar',
long_description='SWIG interface to constraint_grammar for use in apertium-python',
description='SWIG interface to CG-3',
long_description='SWIG interface to CG-3 for use in apertium-python',
# TODO: author, author_email, maintainer, url
license='GPL-3.0+',
cmdclass={'build': CustomBuild},
ext_modules=[constraint_grammar_module],
py_modules=['constraint_grammar', 'cg3'],
)
97 changes: 44 additions & 53 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,67 @@ macro(cg3_link target)
)
endmacro()

set(LIBCG3_HEADERS
set(LIBCG3_SOURCES
AST.hpp
ApertiumApplicator.cpp
ApertiumApplicator.hpp
BinaryGrammar.cpp
BinaryGrammar.hpp
BinaryGrammar_read.cpp
BinaryGrammar_read_10043.cpp
BinaryGrammar_write.cpp
Cohort.cpp
Cohort.hpp
CohortIterator.cpp
CohortIterator.hpp
ContextualTest.cpp
ContextualTest.hpp
FSTApplicator.cpp
FSTApplicator.hpp
FormatConverter.cpp
FormatConverter.hpp
Grammar.cpp
Grammar.hpp
GrammarApplicator.cpp
GrammarApplicator.hpp
GrammarApplicator_context.cpp
GrammarApplicator_matchSet.cpp
GrammarApplicator_reflow.cpp
GrammarApplicator_runContextualTest.cpp
GrammarApplicator_runGrammar.cpp
GrammarApplicator_runRules.cpp
GrammarWriter.cpp
GrammarWriter.hpp
Relabeller.hpp
IGrammarParser.hpp
MathParser.hpp
MatxinApplicator.cpp
MatxinApplicator.hpp
MweSplitApplicator.cpp
MweSplitApplicator.hpp
IGrammarParser.hpp
NicelineApplicator.cpp
NicelineApplicator.hpp
PlaintextApplicator.cpp
PlaintextApplicator.hpp
Profiler.cpp
Profiler.hpp
Reading.cpp
Reading.hpp
Relabeller.cpp
Relabeller.hpp
Rule.cpp
Rule.hpp
Set.cpp
Set.hpp
SingleWindow.cpp
SingleWindow.hpp
Strings.cpp
Strings.hpp
Tag.cpp
Tag.hpp
TagTrie.hpp
TextualParser.cpp
TextualParser.hpp
Window.cpp
Window.hpp
bloomish.hpp
cg3.h
Expand All @@ -92,40 +131,10 @@ set(LIBCG3_HEADERS
sorted_vector.hpp
stdafx.hpp
streambuf.hpp
uextras.cpp
uextras.hpp
version.hpp
)
set(LIBCG3_SOURCES
BinaryGrammar.cpp
BinaryGrammar_read.cpp
BinaryGrammar_read_10043.cpp
BinaryGrammar_write.cpp
Cohort.cpp
CohortIterator.cpp
ContextualTest.cpp
Grammar.cpp
GrammarApplicator.cpp
GrammarApplicator_context.cpp
GrammarApplicator_matchSet.cpp
GrammarApplicator_reflow.cpp
GrammarApplicator_runContextualTest.cpp
GrammarApplicator_runGrammar.cpp
GrammarApplicator_runRules.cpp
GrammarWriter.cpp
Relabeller.cpp
MweSplitApplicator.cpp
Profiler.cpp
Reading.cpp
Rule.cpp
Set.cpp
SingleWindow.cpp
Strings.cpp
Tag.cpp
TextualParser.cpp
Window.cpp
uextras.cpp
${POPEN_PLUS_CPP}
${LIBCG3_HEADERS}
)
if(WIN32 OR EMSCRIPTEN)
set(LIBCG3_SOURCES
Expand All @@ -151,13 +160,7 @@ add_executable(cg-comp cg-comp.cpp)
cg3_link(cg-comp)
target_link_libraries(cg-comp ${LINKLIB})

add_executable(cg-proc
cg-proc.cpp
ApertiumApplicator.cpp
ApertiumApplicator.hpp
MatxinApplicator.cpp
MatxinApplicator.hpp
)
add_executable(cg-proc cg-proc.cpp)
cg3_link(cg-proc)
target_link_libraries(cg-proc ${LINKLIB})

Expand All @@ -167,18 +170,6 @@ target_link_libraries(cg-relabel ${LINKLIB})

add_executable(cg-conv
cg-conv.cpp
ApertiumApplicator.cpp
ApertiumApplicator.hpp
MatxinApplicator.cpp
MatxinApplicator.hpp
FormatConverter.cpp
FormatConverter.hpp
FSTApplicator.cpp
FSTApplicator.hpp
NicelineApplicator.cpp
NicelineApplicator.hpp
PlaintextApplicator.cpp
PlaintextApplicator.hpp
options_conv.hpp
options_parser.hpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ constexpr auto CG3_COPYRIGHT_STRING = "Copyright (C) 2007-2023 GrammarSoft ApS.

constexpr uint32_t CG3_VERSION_MAJOR = 1;
constexpr uint32_t CG3_VERSION_MINOR = 4;
constexpr uint32_t CG3_VERSION_PATCH = 7;
constexpr uint32_t CG3_VERSION_PATCH = 8;
constexpr uint32_t CG3_REVISION = 13897;
constexpr uint32_t CG3_FEATURE_REV = 13897;
constexpr uint32_t CG3_TOO_OLD = 10373;
Expand Down

0 comments on commit 83f415f

Please sign in to comment.