From e4a66b786e5268e001f76b6a7ebbb85d53564100 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 1 Jun 2016 20:21:10 -0400 Subject: [PATCH] Fixed static builds, added GTK+ builds, and cleaned out things in general for cmake. --- CMakeLists.txt | 34 ++++++++++----------- darwin/CMakeLists.txt | 29 ++++++++++-------- examples/CMakeLists.txt | 3 ++ unix/CMakeLists.txt | 65 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 unix/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ee46176..fa29f1d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,10 @@ set(CMAKE_OSX_DEPLOYMENT_TARGET "10.8") project(libui) -set(EXECUTABLE_OUTPUT_PATH "${PROJECT_BINARY_DIR}/out") -set(LIBRARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/out") +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/out") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/out") +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/out") +set(CMAKE_PDB_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/out") # now that we called project(), load our config variables macro(cfgcopy _prefix) @@ -58,7 +60,8 @@ if(APPLE) set(_VERSION "A") set(_PLATFORM_LIBS - "-lobjc -framework Foundation -framework AppKit") + -lobjc "-framework Foundation" "-framework AppKit" + ) # always use our rpath set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) @@ -71,26 +74,22 @@ elseif(WIN32) # note that usp10 comes before gdi32 # TODO prune this list - set(_PLATFORM_LIBS_BASE - user32 kernel32 usp10 gdi32 comctl32 uxtheme msimg32 comdlg32 d2d1 dwrite ole32 oleaut32 oleacc uuid) - if(MSVC) - string(REPLACE ";" ".lib " _PLATFORM_LIBS "${_PLATFORM_LIBS_BASE}") - set(_PLATFORM_LIBS "${_PLATFORM_LIBS}.lib") - else() - string(REPLACE ";" " -l" _PLATFORM_LIBS "${_PLATFORM_LIBS_BASE}") - set(_PLATFORM_LIBS "-l${_PLATFORM_LIBS}") - endif() + set(_PLATFORM_LIBS + user32 kernel32 usp10 gdi32 comctl32 uxtheme msimg32 comdlg32 d2d1 dwrite ole32 oleaut32 oleacc uuid + ) else() set(_OSDIR unix) set(_SETVERSION TRUE) set(_VERSION "0") + find_package(PkgConfig REQUIRED) pkg_check_modules(GTK REQUIRED gtk+-3.0) - set(_LIBUI_CFLAGS "${GTK_CFLAGS}") + string(REPLACE ";" " " _LIBUI_CFLAGS "${GTK_CFLAGS}") set(_PLATFORM_LIBS "${GTK_LDFLAGS} -lm -ldl") # always use our rpath set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) + set(CMAKE_INSTALL_RPATH "\$ORIGIN") endif() if(MSVC) @@ -120,11 +119,6 @@ else() # don't amend CMAKE_STATIC_LINKER_FLAGS; that's for ar endif() -# and add the platform libraries to the three places that need it: shared library links and the two static executable links -append(CMAKE_SHARED_LINKER_FLAGS " ${_PLATFORM_LIBS}") -append2(CMAKE_EXE_LINKER_FLAGS_STATIC CMAKE_EXE_LINKER_FLAGS_RUNTIMESTATIC - " ${_PLATFORM_LIBS}") - add_subdirectory("common") add_subdirectory("${_OSDIR}") if(_SHARED) @@ -136,6 +130,7 @@ if(_SHARED) set_target_properties(libui PROPERTIES SOVERSION "${_VERSION}") endif() + target_link_libraries(libui PRIVATE ${_PLATFORM_LIBS}) else() _add_static(libui $ @@ -150,6 +145,9 @@ macro(_add_exec _name) WIN32 EXCLUDE_FROM_ALL ${ARGN}) target_link_libraries(${_name} libui) + if(NOT _SHARED) + target_link_libraries(${_name} ${_PLATFORM_LIBS}) + endif() endmacro() add_subdirectory("test") diff --git a/darwin/CMakeLists.txt b/darwin/CMakeLists.txt index 651a175e..82450544 100644 --- a/darwin/CMakeLists.txt +++ b/darwin/CMakeLists.txt @@ -42,21 +42,26 @@ set_target_properties(libui-darwin PROPERTIES COMPILE_FLAGS "${_LIBUI_CFLAGS}" ) +# thanks to Mr-Hide in irc.freenode.net/#cmake macro(_add_static _name) - add_library(${_name} STATIC "${ARGN}") - file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/sharedhidden) + add_library(${_name}-temporary STATIC "${ARGN}") + set_target_properties(${_name}-temporary PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") + set(_aname $) + set(_lname ${_name}-combined.list) + set(_oname ${_name}-combined.o) add_custom_command( - TARGET ${_name} POST_BUILD + OUTPUT ${_oname} COMMAND - ${CMAKE_AR} x $ + nm -m ${_aname} | sed -E -n "'s/^[0-9a-f]* \\([A-Z_]+,[a-z_]+\\) external //p'" > ${_lname} COMMAND - nm -m *.o | sed -E -n "'s/^[0-9a-f]* \\([A-Z_]+,[a-z_]+\\) external //p'" > ${_name}.lst - COMMAND - ld -exported_symbols_list ${_name}.lst -r *.o -o ../_combined_${_name}.o - COMMAND - rm $ - COMMAND - ${CMAKE_AR} rcs $ ../_combined_${_name}.o - WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/sharedhidden + ld -exported_symbols_list ${_lname} -r -all_load ${_aname} -o ${_oname} COMMENT "Removing hidden symbols") + add_library(${_name} STATIC ${_oname}) + # otherwise cmake won't know which linker to use + set_target_properties(${_name} PROPERTIES + LINKER_LANGUAGE C) + set(_aname) + set(_lname) + set(_oname) endmacro() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 64f35869..af34dfa0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,12 +5,15 @@ include_directories(..) _add_exec(controlgallery controlgallery/main.c ) + _add_exec(histogram histogram/main.c ) + _add_exec(cpp-multithread cpp-multithread/main.cpp ) +target_link_libraries(cpp-multithread pthread) add_custom_target(examples DEPENDS diff --git a/unix/CMakeLists.txt b/unix/CMakeLists.txt new file mode 100644 index 00000000..b98091ef --- /dev/null +++ b/unix/CMakeLists.txt @@ -0,0 +1,65 @@ +# 1 june 2016 + +include_directories(.. . ../common) + +add_library(libui-unix OBJECT + alloc.c + area.c + box.c + button.c + checkbox.c + child.c + colorbutton.c + combobox.c + control.c + datetimepicker.c + debug.c + draw.c + drawmatrix.c + drawpath.c + drawtext.c + editablecombo.c + entry.c + fontbutton.c + graphemes.c + group.c + label.c + main.c + menu.c + multilineentry.c + progressbar.c + radiobuttons.c + separator.c + slider.c + spinbox.c + stddialogs.c + tab.c + text.c + util.c + window.c +) +set_target_properties(libui-unix PROPERTIES + COMPILE_FLAGS "${_LIBUI_CFLAGS}" +) + +# thanks to Mr-Hide in irc.freenode.net/#cmake +macro(_add_static _name) + add_library(${_name}-temporary STATIC "${ARGN}") + set_target_properties(${_name}-temporary PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}") + set(_aname $) + set(_oname ${_name}-combined.o) + add_custom_command( + OUTPUT ${_oname} + COMMAND + ld -r --whole-archive ${_aname} -o ${_oname} + COMMAND + objcopy --localize-hidden ${_oname} + COMMENT "Removing hidden symbols") + add_library(${_name} STATIC ${_oname}) + # otherwise cmake won't know which linker to use + set_target_properties(${_name} PROPERTIES + LINKER_LANGUAGE C) + set(_aname) + set(_oname) +endmacro()