Skip to content

Commit

Permalink
Add support for building shared library with CMake (#291)
Browse files Browse the repository at this point in the history
* Add support to build openlibm as a shared library using cmake by not compiling the C implementation when a native one exists

* Add CMake static and shared library build instruction and also added info about support for loongarch64

---------

Co-authored-by: Viral B. Shah <ViralBShah@users.noreply.github.com>
  • Loading branch information
ryonaldteofilo and ViralBShah authored Jan 11, 2024
1 parent bd591aa commit b31c645
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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.

0 comments on commit b31c645

Please sign in to comment.