Skip to content

Commit

Permalink
Merge pull request #567 from chewing/clean-up-c99-code
Browse files Browse the repository at this point in the history
build!: Remove most unused C code
  • Loading branch information
kanru committed May 26, 2024
2 parents e9a0848 + c0d6908 commit a77a8dc
Show file tree
Hide file tree
Showing 75 changed files with 1,193 additions and 14,695 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
preset:
- rust-release
- rust-with-sqlite-release
- c99-release
- c99-with-uhash-release
runs-on: ${{ matrix.os }}
name: ${{ matrix.os }}, preset=${{ matrix.preset }}

Expand Down Expand Up @@ -79,8 +77,6 @@ jobs:
preset:
- rust-coverage
- rust-with-sqlite-coverage
- c99-coverage
- c99-with-uhash-coverage
runs-on: ubuntu-latest
container: fedora:latest
name: Coverage preset=${{ matrix.preset }}
Expand Down
216 changes: 45 additions & 171 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,14 @@ if(CMAKE_C_COMPILER_ID MATCHES GNU|Clang)
add_compile_definitions(_GNU_SOURCE)
option(ENABLE_GCOV "Coverage support" false)
if(ENABLE_GCOV)
if(WITH_RUST AND CMAKE_C_COMPILER_ID MATCHES Clang)
if(CMAKE_C_COMPILER_ID MATCHES Clang)
set(CMAKE_C_FLAGS "-fprofile-instr-generate -fcoverage-mapping ${CMAKE_C_FLAGS}")
else()
set(CMAKE_C_FLAGS "--coverage ${CMAKE_C_FLAGS}")
endif()
endif()

# Use NO_UNDEFINED=no when running with address sanitizer
option(NO_UNDEFINED "No undefined symbol in object file" true)
if(NO_UNDEFINED)
set(saved_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
set(CMAKE_REQUIRED_FLAGS "-Wl,--no-undefined")
check_c_compiler_flag("" HAVE_NO_UNDEFINED)
set(CMAKE_REQUIRED_FLAGS ${saved_CMAKE_REQUIRED_FLAGS})

if(HAVE_NO_UNDEFINED)
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-undefined ${CMAKE_SHARED_LINKER_FLAGS}")
endif()
endif()
elseif(MSVC)
# /wd4819
# Without BOM, Visual Studio does not treat source file as UTF-8
# encoding, thus it will complain about invalid character. Use
# /wd4819 can suppress this warning.
set(CMAKE_C_FLAGS "/wd4819 ${CMAKE_C_FLAGS}")
add_compile_definitions(_CRT_SECURE_NO_WARNINGS _CRT_NONSTDC_NO_DEPRECATE)
add_compile_definitions(__func__=__FUNCTION__)

# MSVC 2015 supports `snprintf`, so no need to redefine it
if(MSVC_VERSION LESS 1900)
add_compile_definitions(snprintf=_snprintf)
endif()
endif()


check_c_compiler_flag(-fvisibility=hidden FVISIBILITY_HIDDEN)
if(${FVISIBILITY_HIDDEN})
set(CMAKE_C_FLAGS "-fvisibility=hidden ${CMAKE_C_FLAGS}")
endif()

# automake compatibility
add_compile_definitions(HAVE_CONFIG_H=1)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND})

Expand All @@ -78,31 +45,29 @@ option(WITH_SQLITE3 "Use sqlite3 to store userphrase" true)
# Use valgrind when testing
option(USE_VALGRIND "Use valgrind when testing" true)

option(WITH_RUST "Use rust implemented internals" true)
if(WITH_RUST)
find_package(Corrosion QUIET)
if(NOT Corrosion_FOUND)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG be76480232216a64f65e3b1d9794d68cbac6c690 # v0.4.5
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(Corrosion)
endif()
find_package(Corrosion QUIET)
if(NOT Corrosion_FOUND)
FetchContent_Declare(
Corrosion
GIT_REPOSITORY https://github.com/corrosion-rs/corrosion.git
GIT_TAG be76480232216a64f65e3b1d9794d68cbac6c690 # v0.4.5
FIND_PACKAGE_ARGS
)
FetchContent_MakeAvailable(Corrosion)
endif()

corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing_capi)
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing_testhelper)
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing-cli)
add_compile_definitions(WITH_RUST)
if(WITH_SQLITE3)
corrosion_set_features(chewing_capi FEATURES sqlite)
corrosion_set_features(chewing_testhelper FEATURES sqlite)
endif()
if(ENABLE_GCOV)
corrosion_set_env_vars(chewing_capi CARGO_INCREMENTAL=0)
corrosion_add_target_local_rustflags(chewing_capi -Cinstrument-coverage -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort)
endif()
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing_capi)
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing_testhelper)
corrosion_import_crate(MANIFEST_PATH Cargo.toml CRATES chewing-cli)

if(WITH_SQLITE3)
corrosion_set_features(chewing_capi FEATURES sqlite)
corrosion_set_features(chewing_testhelper FEATURES sqlite)
endif()

if(ENABLE_GCOV)
corrosion_set_env_vars(chewing_capi CARGO_INCREMENTAL=0)
corrosion_add_target_local_rustflags(chewing_capi -Cinstrument-coverage -Ccodegen-units=1 -Cinline-threshold=0 -Clink-dead-code -Coverflow-checks=off -Cpanic=abort)
endif()

# Feature probe
Expand Down Expand Up @@ -150,16 +115,12 @@ if(WITH_SQLITE3)
endif()

include(CheckFunctionExists)
check_function_exists(strtok_r HAVE_STRTOK_R)
check_function_exists(asprintf HAVE_ASPRINTF)

include(CheckIncludeFiles)
check_include_files(unistd.h HAVE_UNISTD_H)
check_include_files(stdint.h HAVE_STDINT_H)

include(TestBigEndian)
test_big_endian(WORDS_BIGENDIAN)

set(SRC_DIR ${PROJECT_SOURCE_DIR}/src)
set(INC_DIR ${PROJECT_SOURCE_DIR}/include)
set(TOOLS_SRC_DIR ${PROJECT_SOURCE_DIR}/src/tools)
Expand Down Expand Up @@ -197,40 +158,20 @@ configure_file(
include_directories(
${PROJECT_BINARY_DIR}/include
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/include/internal
${PROJECT_SOURCE_DIR}/src
${PROJECT_SOURCE_DIR}/src/porting_layer/include
)

set(ALL_INC
${INC_DIR}/chewing.h
${INC_DIR}/chewing-compat.h
${INC_DIR}/chewingio.h
${INC_DIR}/global.h
${INC_DIR}/mod_aux.h
)

if(WITH_RUST)
list(APPEND ALL_INC ${INC_DIR}/chewing_rs.h)
endif()

add_subdirectory(doc)
add_subdirectory(data)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()

# library
add_library(common OBJECT
src/porting_layer/src/asprintf.h

src/porting_layer/src/asprintf.c
)
target_compile_definitions(common PRIVATE
CHEWING_DATADIR=\"${CMAKE_INSTALL_FULL_DATADIR}/libchewing\"
)

add_library(libchewing ${ALL_INC} src/chewing.c)
add_library(libchewing ${ALL_INC} capi/src/chewing.c)
set_target_properties(libchewing PROPERTIES LINKER_LANGUAGE C)
target_compile_definitions(libchewing PRIVATE
CHEWING_DATADIR=\"${CMAKE_INSTALL_FULL_DATADIR}/libchewing\"
Expand All @@ -240,94 +181,29 @@ target_include_directories(libchewing
$<BUILD_INTERFACE:${INC_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/chewing>
)
if(NOT WITH_RUST)
target_sources(common PRIVATE
${SRC_DIR}/porting_layer/include/plat_mmap.h
${SRC_DIR}/porting_layer/include/plat_path.h
${SRC_DIR}/porting_layer/include/plat_types.h
${SRC_DIR}/porting_layer/include/sys/plat_posix.h
${SRC_DIR}/porting_layer/include/sys/plat_windows.h

${SRC_DIR}/porting_layer/src/plat_mmap_posix.c
${SRC_DIR}/porting_layer/src/plat_mmap_windows.c
${SRC_DIR}/porting_layer/src/plat_path.c
${SRC_DIR}/porting_layer/src/rpl_malloc.c
${SRC_DIR}/common/chewing-utf8-util.c
${SRC_DIR}/common/key2pho.c
)
target_sources(libchewing PRIVATE
${INC_DIR}/internal/bopomofo-private.h
${INC_DIR}/internal/chewing-private.h
${INC_DIR}/internal/chewingutil.h
${INC_DIR}/internal/choice-private.h
${INC_DIR}/internal/dict-private.h
${INC_DIR}/internal/global-private.h
${INC_DIR}/internal/pinyin-private.h
${INC_DIR}/internal/tree-private.h
${INC_DIR}/internal/userphrase-private.h

${SRC_DIR}/bopomofo.c
${SRC_DIR}/chewingio.c
${SRC_DIR}/chewingutil.c
${SRC_DIR}/choice.c
${SRC_DIR}/compat.c
${SRC_DIR}/dict.c
${SRC_DIR}/mod_aux.c
${SRC_DIR}/pinyin.c
${SRC_DIR}/private.h
${SRC_DIR}/tree.c
${SRC_DIR}/userphrase.c
)
if(WITH_SQLITE3)
add_library(userphrase OBJECT
${INC_DIR}/internal/chewing-sql.h
${SRC_DIR}/chewing-sql.c
${SRC_DIR}/userphrase-sql.c

corrosion_set_env_vars(chewing_capi
CHEWING_DATADIR=${CMAKE_INSTALL_FULL_DATADIR}/libchewing
)
target_link_libraries(libchewing PRIVATE chewing_capi)
target_link_libraries(chewing_capi INTERFACE ${SQLite3_LIBRARIES})
if(BUILD_SHARED_LIBS)
if(CMAKE_C_COMPILER_ID MATCHES GNU|^Clang)
target_link_options(libchewing
PRIVATE LINKER:-version-script,${PROJECT_SOURCE_DIR}/capi/src/symbols-elf.map
PRIVATE LINKER:--gc-sections
PRIVATE LINKER:-u,chewing_new
)
else()
add_library(userphrase OBJECT
${INC_DIR}/internal/hash-private.h
${SRC_DIR}/hash.c
${SRC_DIR}/userphrase-hash.c
elseif(CMAKE_C_COMPILER_ID MATCHES AppleClang)
target_link_options(libchewing
PRIVATE LINKER:-exported_symbols_list,${PROJECT_SOURCE_DIR}/capi/src/symbols-mach_o.map
PRIVATE LINKER:-dead_strip
)
elseif(MSVC)
target_link_options(libchewing
PRIVATE /DEF:${PROJECT_SOURCE_DIR}/capi/src/symbols-msvc.def)
set_target_properties(libchewing PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
target_link_libraries(libchewing
PRIVATE common
PRIVATE userphrase)
if(BUILD_SHARED_LIBS AND MSVC)
target_compile_definitions(libchewing PRIVATE CHEWINGDLL_EXPORTS)
endif()
endif()

if(WITH_RUST)
corrosion_set_env_vars(chewing_capi
CHEWING_DATADIR=${CMAKE_INSTALL_FULL_DATADIR}/libchewing
)
target_link_libraries(libchewing PRIVATE chewing_capi)
target_link_libraries(chewing_capi INTERFACE ${SQLite3_LIBRARIES})
if(BUILD_SHARED_LIBS)
if(CMAKE_C_COMPILER_ID MATCHES GNU|^Clang)
target_link_options(libchewing
PRIVATE LINKER:-version-script,${PROJECT_SOURCE_DIR}/capi/src/symbols-elf.map
PRIVATE LINKER:--gc-sections
PRIVATE LINKER:-u,chewing_new
PRIVATE LINKER:-u,ueStrLen
PRIVATE LINKER:-u,UintFromPhone
PRIVATE LINKER:-u,find_path_by_files
)
elseif(CMAKE_C_COMPILER_ID MATCHES AppleClang)
target_link_options(libchewing
PRIVATE LINKER:-exported_symbols_list,${PROJECT_SOURCE_DIR}/capi/src/symbols-mach_o.map
PRIVATE LINKER:-dead_strip
)
elseif(MSVC)
target_link_options(libchewing
PRIVATE /DEF:${PROJECT_SOURCE_DIR}/capi/src/symbols-msvc.def)
set_target_properties(libchewing PROPERTIES MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()
endif()
elseif(WITH_SQLITE3)
target_link_libraries(libchewing PRIVATE ${SQLite3_LIBRARIES})
endif()

if(MSVC)
Expand All @@ -351,9 +227,7 @@ install(TARGETS libchewing
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
if(WITH_RUST)
install(IMPORTED_RUNTIME_ARTIFACTS chewing-cli DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
install(IMPORTED_RUNTIME_ARTIFACTS chewing-cli DESTINATION ${CMAKE_INSTALL_BINDIR})

# generate CMake Config files
include(CMakePackageConfigHelpers)
Expand Down
67 changes: 0 additions & 67 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,73 +98,6 @@
"rust-with-sqlite",
"coverage-base"
]
},
{
"name": "c99",
"displayName": "C99 (Debug)",
"description": "Build libchewing with only C dependencies",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"BUILD_INFO": true,
"WITH_RUST": false,
"WITH_SQLITE3": true
},
"inherits": [
"build-base"
]
},
{
"name": "c99-release",
"displayName": "C99 (Release)",
"description": "Build libchewing with only C dependencies",
"inherits": [
"c99"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "c99-coverage",
"displayName": "C99 (Debug Coverage)",
"description": "Build libchewing with only C dependencies",
"inherits": [
"c99",
"coverage-base"
]
},
{
"name": "c99-with-uhash",
"displayName": "C99 with uhash (Debug)",
"description": "Build libchewing with C and uhash user dictionary format",
"inherits": [
"c99"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"WITH_RUST": false,
"WITH_SQLITE3": false
}
},
{
"name": "c99-with-uhash-release",
"displayName": "C99 with uhash (Release)",
"description": "Build libchewing with C and uhash dictionary format",
"inherits": [
"c99-with-uhash"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "c99-with-uhash-coverage",
"displayName": "C99 with uhash (Debug Coverage)",
"description": "Build libchewing with C and uhash dictionary format",
"inherits": [
"c99-with-uhash",
"coverage-base"
]
}
],
"buildPresets": [
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ log = "0.4.21"
tempfile = "3.10.1"

[profile.release]
lto = true
opt-level = 3
panic = "abort"
codegen-units = 1
debug = true
Expand Down
Loading

0 comments on commit a77a8dc

Please sign in to comment.