Skip to content

Commit b31c645

Browse files
Add support for building shared library with CMake (#291)
* 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 <[email protected]>
1 parent bd591aa commit b31c645

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

CMakeLists.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,32 @@ else()
503503
message(FATAL_ERROR "${PROJECT_NAME} CMake build is not set up for ${OPENLIBM_ARCH_FOLDER}")
504504
endif()
505505

506+
507+
# Filter out C implementation from compilation list if a native implementation exists
508+
# when generating a shared library to avoid multiple definitions
509+
get_target_property(TARGET_TYPE "${PROJECT_NAME}" TYPE)
510+
if (TARGET_TYPE STREQUAL SHARED_LIBRARY)
511+
foreach(FILE_TO_REMOVE ${OPENLIBM_ASM_SOURCE})
512+
# Get filename and strip out extension
513+
cmake_path(GET FILE_TO_REMOVE FILENAME FILENAME_TO_REMOVE)
514+
cmake_path(REMOVE_EXTENSION FILENAME_TO_REMOVE OUTPUT_VARIABLE FILENAME_TO_REMOVE)
515+
message(DEBUG "Filename to remove: ${FILENAME_TO_REMOVE}")
516+
517+
# Go through files and remove one with the same name
518+
foreach(CUR_FILE ${OPENLIBM_C_SOURCE})
519+
cmake_path(GET CUR_FILE FILENAME CUR_FILENAME)
520+
cmake_path(REMOVE_EXTENSION CUR_FILENAME OUTPUT_VARIABLE CUR_FILENAME)
521+
522+
if(${CUR_FILENAME} STREQUAL ${FILENAME_TO_REMOVE})
523+
list(REMOVE_ITEM OPENLIBM_C_SOURCE ${CUR_FILE})
524+
message(DEBUG "Removed source file from compilation list: ${CUR_FILE}")
525+
break()
526+
endif()
527+
endforeach()
528+
endforeach()
529+
endif()
530+
531+
506532
# Add sources
507533
target_sources("${PROJECT_NAME}" PRIVATE ${OPENLIBM_C_SOURCE}
508534
${OPENLIBM_ASM_SOURCE}

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ consistently across compilers and operating systems, and in 32-bit and
1818
OpenLibm builds on Linux, macOS, Windows, FreeBSD, OpenBSD, NetBSD, and
1919
DragonFly BSD. It builds with both GCC and clang. Although largely
2020
tested and widely used on the x86 and x86-64 architectures, OpenLibm
21-
also supports arm, aarch64, ppc64le, mips, wasm32, riscv, s390(x), and
21+
also supports arm, aarch64, ppc64le, mips, wasm32, riscv, s390(x) and
2222
loongarch64.
2323

2424
## Build instructions
2525

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

39+
### CMake
40+
41+
1. Create build directory with `mkdir build` and navigate into it with `cd build`.
42+
2. Run CMake to configure project and generate native build system with `cmake /path/to/openlibm/`
43+
or generate project with build system of choice e.g. `cmake /path/to/openlib/ -G "MinGW Makefiles"`.
44+
3. Build with the build system with `cmake --build .`.
45+
46+
Default CMake configuration builds a `STATIC` library. To build a `SHARED` library,
47+
replace the keyword the `add_library()`in the `CMakeLists.txt`.
48+
49+
3750
## Acknowledgements
3851

3952
PowerPC support for openlibm was graciously sponsored by IBM.

0 commit comments

Comments
 (0)