Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for building shared library with CMake #291

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,32 @@ else()
message(FATAL_ERROR "${PROJECT_NAME} CMake build is not set up for ${OPENLIBM_ARCH_FOLDER}")
endif()


# Filter out C implementation from compilation list if a native implementation exists
# when generating a shared library to avoid multiple definitions
get_target_property(TARGET_TYPE "${PROJECT_NAME}" TYPE)
if (TARGET_TYPE STREQUAL SHARED_LIBRARY)
foreach(FILE_TO_REMOVE ${OPENLIBM_ASM_SOURCE})
# Get filename and strip out extension
cmake_path(GET FILE_TO_REMOVE FILENAME FILENAME_TO_REMOVE)
cmake_path(REMOVE_EXTENSION FILENAME_TO_REMOVE OUTPUT_VARIABLE FILENAME_TO_REMOVE)
message(DEBUG "Filename to remove: ${FILENAME_TO_REMOVE}")

# Go through files and remove one with the same name
foreach(CUR_FILE ${OPENLIBM_C_SOURCE})
cmake_path(GET CUR_FILE FILENAME CUR_FILENAME)
cmake_path(REMOVE_EXTENSION CUR_FILENAME OUTPUT_VARIABLE CUR_FILENAME)

if(${CUR_FILENAME} STREQUAL ${FILENAME_TO_REMOVE})
list(REMOVE_ITEM OPENLIBM_C_SOURCE ${CUR_FILE})
message(DEBUG "Removed source file from compilation list: ${CUR_FILE}")
break()
endif()
endforeach()
endforeach()
endif()


# Add sources
target_sources("${PROJECT_NAME}" PRIVATE ${OPENLIBM_C_SOURCE}
${OPENLIBM_ASM_SOURCE}
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ consistently across compilers and operating systems, and in 32-bit and
OpenLibm builds on Linux, macOS, Windows, FreeBSD, OpenBSD, NetBSD, and
DragonFly BSD. It builds with both GCC and clang. Although largely
tested and widely used on the x86 and x86-64 architectures, OpenLibm
also supports arm, aarch64, ppc64le, mips, wasm32, riscv, s390(x), and
also supports arm, aarch64, ppc64le, mips, wasm32, riscv, s390(x) and
loongarch64.

## Build instructions

### GNU Make

1. Use GNU Make to build OpenLibm. This is `make` on most systems, but `gmake` on BSDs.
2. Use `make USEGCC=1` to build with GCC. This is the default on
Linux and Windows.
Expand All @@ -34,6 +36,17 @@ loongarch64.
i686. GCC 4.8 is the minimum requirement for correct codegen on
older 32-bit architectures.

### CMake

1. Create build directory with `mkdir build` and navigate into it with `cd build`.
2. Run CMake to configure project and generate native build system with `cmake /path/to/openlibm/`
or generate project with build system of choice e.g. `cmake /path/to/openlib/ -G "MinGW Makefiles"`.
3. Build with the build system with `cmake --build .`.

Default CMake configuration builds a `STATIC` library. To build a `SHARED` library,
replace the keyword the `add_library()`in the `CMakeLists.txt`.


## Acknowledgements

PowerPC support for openlibm was graciously sponsored by IBM.
Loading