From 3cb02f312447731b5a949aca388d43b651b53a0d Mon Sep 17 00:00:00 2001
From: Alessandro Barbieri <lssndrbarbieri@gmail.com>
Date: Sat, 30 Oct 2021 20:28:49 +0200
Subject: [PATCH 1/2] build libscr_base.so

--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -94,8 +94,24 @@ SET_TARGET_PROPERTIES(scr-static PROPERTIES OUTPUT_NAME scr CLEAN_DIRECT_OUTPUT
 INSTALL(TARGETS scr-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
 # Non-MPI library for CLI
-ADD_LIBRARY(scr_base STATIC ${cliscr_noMPI_srcs})
-TARGET_LINK_LIBRARIES(scr_base ${SCR_EXTERNAL_SERIAL_LIBS})
+ADD_LIBRARY(cliscr_noMPI_o OBJECT ${cliscr_noMPI_srcs})
+
+IF(BUILD_SHARED_LIBS)
+   ADD_LIBRARY(scr_base SHARED $<TARGET_OBJECTS:cliscr_noMPI_o>)
+   TARGET_LINK_LIBRARIES(scr_base ${SCR_EXTERNAL_SERIAL_LIBS})
+   SET_TARGET_PROPERTIES(scr_base PROPERTIES OUTPUT_NAME scr_base CLEAN_DIRECT_OUTPUT 1)
+   INSTALL(TARGETS scr_base DESTINATION ${CMAKE_INSTALL_LIBDIR})
+ENDIF(BUILD_SHARED_LIBS)
+
+ADD_LIBRARY(scr_base-static STATIC $<TARGET_OBJECTS:cliscr_noMPI_o>)
+IF(SCR_LINK_STATIC)
+    SET_TARGET_PROPERTIES(scr_base-static PROPERTIES LINK_SEARCH_START_STATIC 1)
+    SET_TARGET_PROPERTIES(scr_base-static PROPERTIES LINK_SEARCH_END_STATIC 1)
+ENDIF(SCR_LINK_STATIC)
+TARGET_LINK_LIBRARIES(scr_base-static ${SCR_EXTERNAL_SERIAL_LIBS})
+
+SET_TARGET_PROPERTIES(scr_base-static PROPERTIES OUTPUT_NAME scr_base CLEAN_DIRECT_OUTPUT 1)
+INSTALL(TARGETS scr_base-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
 # Fortran
 IF(ENABLE_FORTRAN)
@@ -147,9 +163,13 @@ LIST(APPEND cliscr_c_bins
 
 # Build and install C binaries
 FOREACH(bin IN ITEMS ${cliscr_c_bins})
-	ADD_EXECUTABLE(${bin} ${bin}.c)
-	TARGET_LINK_LIBRARIES(${bin} scr_base)
-	INSTALL(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${bin} DESTINATION ${CMAKE_INSTALL_BINDIR})
+    ADD_EXECUTABLE(${bin} ${bin}.c)
+    IF(SCR_LINK_STATIC)
+        TARGET_LINK_LIBRARIES(${bin} scr_base-static)
+    ELSE(SCR_LINK_STATIC)
+        TARGET_LINK_LIBRARIES(${bin} scr_base)
+    ENDIF(SCR_LINK_STATIC)
+    INSTALL(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${bin} DESTINATION ${CMAKE_INSTALL_BINDIR})
 ENDFOREACH(bin IN ITEMS ${cliscr_c_bins})
 
 # Build and install CLI binaries that link full SCR library

From e3d50ca7cab309273f3195c71af14352059d83aa Mon Sep 17 00:00:00 2001
From: Alessandro Barbieri <lssndrbarbieri@gmail.com>
Date: Sat, 30 Oct 2021 20:55:07 +0200
Subject: [PATCH 2/2] remove source files of cli binaries from library

to avoid `multiple definition of main`
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -32,9 +32,6 @@ LIST(APPEND cliscr_noMPI_srcs
 	scr_meta.c
 	scr_param.c
 	scr_util.c
-	scr_rebuild_xor.c
-	scr_rebuild_partner.c
-	scr_rebuild_rs.c
 )
 
 LIST(APPEND libscr_srcs
