From: Douglas RAILLARD <douglas.raillard@arm.com>
To: acme@redhat.com
Cc: dwarves@vger.kernel.org, douglas.raillard@arm.com
Subject: [PATCH] CMakeLists.txt: Add STATIC_LINK option
Date: Fri, 15 Oct 2021 11:10:08 +0100 [thread overview]
Message-ID: <20211015101008.486302-1-douglas.raillard@arm.com> (raw)
From: Douglas Raillard <douglas.raillard@arm.com>
Add a user-defined STATIC_LINK option that can be used to build a fully
static binary for the executables:
cmake .. -DSTATIC_LINK=ON
This has been tested on Alpine Linux v3.14.
Signed-off-by: Douglas Raillard <douglas.raillard@arm.com>
---
CMakeLists.txt | 21 +++++++++++++++------
README | 3 +++
cmake/modules/FindDWARF.cmake | 10 ++++++++++
3 files changed, 28 insertions(+), 6 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2ab66e4..ba467bf 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,6 +34,15 @@ macro(_set_fancy _var _value _comment)
endif (NOT DEFINED ${_var})
endmacro(_set_fancy)
+option(BUILD_SHARED_LIBS "Build internal libraries as shared libraries" ON)
+option(STATIC_LINK "Create statically linked executables" OFF)
+if (STATIC_LINK)
+ string(APPEND CMAKE_C_FLAGS " -static")
+ string(APPEND CMAKE_EXE_LINKER_FLAGS " -static")
+ set(CMAKE_FIND_LIBRARY_SUFFIXES .a)
+ set(BUILD_SHARED_LIBS OFF)
+endif()
+
# where to look first for cmake modules,
# before ${CMAKE_ROOT}/Modules/ is checked
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/modules")
@@ -48,11 +57,6 @@ set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -ggdb -O0")
set(CMAKE_C_FLAGS_RELEASE "-Wall -O2")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
-if (NOT DEFINED BUILD_SHARED_LIBS)
- set (BUILD_SHARED_LIBS ON)
- message(STATUS "Setting BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")
-endif (NOT DEFINED BUILD_SHARED_LIBS)
-
# Just for grepping, DWARVES_VERSION isn't used anywhere anymore
# add_definitions(-D_GNU_SOURCE -DDWARVES_VERSION="v1.22")
add_definitions(-D_GNU_SOURCE -DDWARVES_MAJOR_VERSION=1)
@@ -123,7 +127,7 @@ endif()
add_library(dwarves ${dwarves_LIB_SRCS})
set_target_properties(dwarves PROPERTIES VERSION 1.0.0 SOVERSION 1)
set_target_properties(dwarves PROPERTIES INTERFACE_LINK_LIBRARIES "")
-target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY})
+target_link_libraries(dwarves ${DWARF_LIBRARIES} ${ZLIB_LIBRARIES} ${LIBBPF_LIBRARIES} ${ARGP_LIBRARY} ${OBSTACK_LIBRARY} ${BZ2_LIBRARY} ${LZMA_LIBRARY})
set(dwarves_emit_LIB_SRCS dwarves_emit.c)
add_library(dwarves_emit ${dwarves_emit_LIB_SRCS})
@@ -193,3 +197,8 @@ endif()
install(PROGRAMS btfdiff fullcircle DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
install(FILES lib/Makefile lib/ctracer_relay.c lib/ctracer_relay.h lib/linux.blacklist.cu
DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dwarves/runtime)
+
+# Avoid having a trailing -Wl,-Bdynamic that will make some linkers think we
+# need to link against a DSO for the libc.
+get_property(TARGETS DIRECTORY PROPERTY BUILDSYSTEM_TARGETS)
+set_target_properties(${TARGETS} PROPERTIES LINK_SEARCH_END_STATIC ${STATIC_LINK})
diff --git a/README b/README
index c9f1737..5798458 100644
--- a/README
+++ b/README
@@ -18,6 +18,9 @@ cmake Options:
Default is to install to /usr/local, use -DCMAKE_INSTALL_PREFIX=
when invoking cmake to specify another install location.
+ -DSTATIC_LINK
+ Build a statically linked binary. Default is OFF.
+
Known to work scenarios:
Mandriva Cooker:
diff --git a/cmake/modules/FindDWARF.cmake b/cmake/modules/FindDWARF.cmake
index 027d06e..1b4ac49 100644
--- a/cmake/modules/FindDWARF.cmake
+++ b/cmake/modules/FindDWARF.cmake
@@ -37,6 +37,16 @@ find_library(ELF_LIBRARY
PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
)
+find_library(BZ2_LIBRARY
+ NAMES bz2
+ PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
+find_library(LZMA_LIBRARY
+ NAMES lzma
+ PATHS /usr/lib /usr/local/lib /usr/lib64 /usr/local/lib64 ~/usr/local/lib ~/usr/local/lib64
+)
+
if (DWARF_INCLUDE_DIR AND LIBDW_INCLUDE_DIR AND DWARF_LIBRARY AND ELF_LIBRARY)
set(DWARF_FOUND TRUE)
set(DWARF_LIBRARIES ${DWARF_LIBRARY} ${ELF_LIBRARY})
--
2.25.1
next reply other threads:[~2021-10-15 10:10 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-15 10:10 Douglas RAILLARD [this message]
2021-10-15 13:18 ` [PATCH] CMakeLists.txt: Add STATIC_LINK option Arnaldo Carvalho de Melo
2021-10-15 13:22 ` Arnaldo Carvalho de Melo
2021-10-15 14:20 ` Arnaldo Carvalho de Melo
2021-10-15 14:39 ` Arnaldo Carvalho de Melo
2021-10-18 9:57 ` Douglas Raillard
2021-10-19 15:24 ` Arnaldo Carvalho de Melo
2021-10-19 16:03 ` Douglas Raillard
2021-10-19 17:29 ` Douglas Raillard
2021-10-20 12:49 ` Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211015101008.486302-1-douglas.raillard@arm.com \
--to=douglas.raillard@arm.com \
--cc=acme@redhat.com \
--cc=dwarves@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).