2016-06-03 13:56:21 -05:00
# 3 june 2016
2016-06-17 08:56:47 -05:00
# see https://cmake.org/gitweb?p=cmake.git;a=commit;h=95cdf132489c79e88a10fdf7a7566fa002c7680b (thanks ngladitz in irc.freenode.net/#cmake)
2016-06-17 09:01:07 -05:00
cmake_minimum_required ( VERSION 3.1.0 FATAL_ERROR )
2016-06-03 13:56:21 -05:00
2016-06-04 14:49:43 -05:00
# TODOs
# - silence entering/leaving messages?
# - uname -s for more refined OS control
# - Haiku for haiku
# - debian DESTDIR? https://github.com/andlabs/libui/pull/10
2016-06-06 17:56:58 -05:00
# - libui-combined* needs to be deleted so that custom command can run every time
2016-06-10 18:34:48 -05:00
# - add notelemetry.obj to *ALL TARGETS* on VS2015 and up - https://www.infoq.com/news/2016/06/visual-cpp-telemetry
2016-06-17 08:56:47 -05:00
# - switch to 3.1.0 features
2016-06-04 14:49:43 -05:00
2016-06-03 20:48:10 -05:00
# the docs say we need to set this up prior to project()
2017-06-02 23:33:40 -05:00
# the docs don't say this, but the CACHE part is important; see https://stackoverflow.com/questions/34208360/cmake-seems-to-ignore-cmake-osx-deployment-target
# TODO figure out what other variables must be set with CACHE
# TODO figure out if FORCE is needed here
# TODO figure out whether STRING "" is best or if something else is better; also what FORCE does because I forget and later I say it's needed
set ( CMAKE_OSX_DEPLOYMENT_TARGET "10.8" CACHE STRING "" FORCE )
2016-06-03 17:28:14 -05:00
2016-06-04 14:31:29 -05:00
# we want to disable incremental linking
# see also:
# - https://github.com/bulletphysics/bullet3/blob/master/CMakeLists.txt#L43
# - https://cmake.org/pipermail/cmake/2010-February/035174.html
# this must also go before project()
set ( MSVC_INCREMENTAL_DEFAULT ON )
2016-06-05 09:43:04 -05:00
# default to debug builds
# do this before project() just to be safe
if ( NOT CMAKE_BUILD_TYPE )
set ( CMAKE_BUILD_TYPE DEBUG CACHE STRING "" FORCE )
endif ( )
2016-06-07 14:09:03 -05:00
project ( libui )
2016-06-03 13:56:21 -05:00
option ( BUILD_SHARED_LIBS "Whether to build libui as a shared library or a static library" ON )
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" )
if ( APPLE )
set ( _OSNAME darwin )
2016-06-03 20:48:10 -05:00
set ( _HASVERSION TRUE )
set ( _VERSION "A" )
# always use our rpath
set ( CMAKE_BUILD_WITH_INSTALL_RPATH TRUE )
# the / is required by some older versions of OS X
set ( CMAKE_INSTALL_RPATH "@executable_path/" )
set ( CMAKE_MACOSX_RPATH TRUE )
2016-06-03 13:56:21 -05:00
elseif ( WIN32 )
set ( _OSNAME windows )
2016-06-03 16:30:00 -05:00
# and don't include the default libraries with ANY of the builds
# note the CACHE FORCE stuff is required here
set ( CMAKE_C_STANDARD_LIBRARIES CACHE STRING "" FORCE )
set ( CMAKE_CXX_STANDARD_LIBRARIES CACHE STRING "" FORCE )
2016-06-03 13:56:21 -05:00
else ( )
set ( _OSNAME unix )
2016-06-03 20:14:55 -05:00
set ( _HASVERSION TRUE )
set ( _VERSION "0" )
# always use our rpath
set ( CMAKE_BUILD_WITH_INSTALL_RPATH TRUE )
set ( CMAKE_INSTALL_RPATH "\$ORIGIN" )
2016-06-03 13:56:21 -05:00
endif ( )
# common flags
if ( MSVC )
# TODO subsystem version
# TODO /Wall does too much
# TODO -Wno-switch equivalent
# TODO /sdl turns C4996 into an ERROR
# don't use /analyze; that requires us to write annotations everywhere
# TODO undecided flags from qo?
# /RTCc is not supplied because it's discouraged as of VS2015; see https://www.reddit.com/r/cpp/comments/46mhne/rtcc_rejects_conformant_code_with_visual_c_2015/d06auq5
# /EHsc is to shut the compiler up in some cases
# TODO make /EHsc C++-only
set ( _COMMON_CFLAGS
/ W 4 / w d 4 1 0 0
/ b i g o b j / n o l o g o
/ R T C 1 / R T C s / R T C u
/ E H s c
)
# note the /MANIFEST:NO (which must be / and uppercase); thanks FraGag (https://github.com/andlabs/libui/issues/93#issuecomment-223183436)
# TODO warnings on undefined symbols
set ( _COMMON_LDFLAGS
/ L A R G E A D D R E S S A W A R E
/ N O L O G O
/ I N C R E M E N T A L : N O
/ M A N I F E S T : N O
)
# TODO autogenerate a .def file?
2016-06-04 14:31:29 -05:00
# more incremental linking fixes
# TODO actually get rid of incremental linking here
2016-06-03 13:56:21 -05:00
else ( )
2016-06-03 19:26:58 -05:00
set ( _COMMON_CFLAGS
- W a l l - W e x t r a - p e d a n t i c
- W n o - u n u s e d - p a r a m e t e r
- W n o - s w i t c h
- f v i s i b i l i t y = h i d d e n
)
# don't use C_VERSION or CXX_VERSION because they use GNU standards
2016-06-17 22:18:31 -05:00
# TODO we can actually do this; set both C_EXTENSIONS and CXX_EXTENSIONS to OFF
2016-06-05 12:48:51 -05:00
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --std=c99" )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11" )
2016-06-03 19:26:58 -05:00
2016-06-04 14:40:59 -05:00
set ( _COMMON_LDFLAGS
- f v i s i b i l i t y = h i d d e n
)
2016-06-05 15:23:01 -05:00
# don't require shipping the MinGW-w64 DLLs
if ( WIN32 )
list ( APPEND _COMMON_LDFLAGS
- s t a t i c
- s t a t i c - l i b g c c
- s t a t i c - l i b s t d c + +
)
endif ( )
2017-02-16 12:00:57 -06:00
# TODO document this
if ( ADDRESS_SANITIZER )
set ( _COMMON_CFLAGS ${ _COMMON_CFLAGS }
- f s a n i t i z e = a d d r e s s
- f n o - o m i t - f r a m e - p o i n t e r
)
set ( _COMMON_LDFLAGS ${ _COMMON_LDFLAGS }
- f s a n i t i z e = a d d r e s s
- f n o - o m i t - f r a m e - p o i n t e r
)
endif ( )
2016-06-03 13:56:21 -05:00
endif ( )
2016-06-03 18:25:43 -05:00
# problem:
# - target_link_libraries() only supports - for flags
# - but cmake only doesn't generate the manifest if the flag has a /
2016-06-03 16:30:00 -05:00
macro ( _target_link_options_private _target )
foreach ( _opt IN LISTS ${ ARGN } )
set_property ( TARGET ${ _target } APPEND_STRING PROPERTY
L I N K _ F L A G S " $ { _ o p t } " )
endforeach ( )
endmacro ( )
2016-06-03 13:56:21 -05:00
add_subdirectory ( "common" )
add_subdirectory ( "${_OSNAME}" )
2018-05-12 22:59:43 -05:00
add_library ( libui ${ _LIBUI_SOURCES } )
target_include_directories ( libui
2016-06-03 13:56:21 -05:00
P U B L I C .
P R I V A T E $ { _ L I B U I _ I N C L U E D I R S } )
2018-05-12 22:59:43 -05:00
target_compile_definitions ( libui
2016-06-03 13:56:21 -05:00
P R I V A T E $ { _ L I B U I _ D E F S } )
2016-06-03 21:19:33 -05:00
# cmake produces this for us by default but only for shared libraries
2018-05-12 22:59:43 -05:00
target_compile_definitions ( libui
2016-06-03 21:19:33 -05:00
P R I V A T E l i b u i _ E X P O R T S )
2018-05-12 22:59:43 -05:00
target_compile_options ( libui
2016-06-03 13:56:21 -05:00
P U B L I C $ { _ C O M M O N _ C F L A G S }
P R I V A T E $ { _ L I B U I _ C F L A G S } )
# TODO link directories?
2016-06-03 17:28:14 -05:00
if ( BUILD_SHARED_LIBS )
2018-05-12 22:59:43 -05:00
target_link_libraries ( libui
2016-06-03 17:28:14 -05:00
P R I V A T E $ { _ L I B U I _ L I B S } )
endif ( )
2016-06-17 09:13:46 -05:00
# TODO INTERFACE libs don't inherit to grandhcildren?
2016-06-03 13:56:21 -05:00
# on Windows the linker for static libraries is different; don't give it the flags
if ( BUILD_SHARED_LIBS )
2018-05-12 22:59:43 -05:00
_target_link_options_private ( libui
2016-06-03 16:30:00 -05:00
_ C O M M O N _ L D F L A G S
_ L I B U I _ L D F L A G S )
2016-06-03 13:56:21 -05:00
endif ( )
if ( NOT BUILD_SHARED_LIBS )
2016-06-04 17:52:40 -05:00
# TODO figure out a way to tell libui that it's static
2018-05-12 22:59:43 -05:00
target_compile_definitions ( libui
2016-06-04 17:52:40 -05:00
P U B L I C _ U I _ S T A T I C )
2016-06-03 13:56:21 -05:00
endif ( )
2016-06-04 13:30:43 -05:00
if ( NOT MSVC )
# on non-MSVC compilers cmake adds an extra lib-
2016-06-03 20:14:55 -05:00
# note that we apply this to libui, not to any intermediates
set_target_properties ( libui PROPERTIES
O U T P U T _ N A M E u i )
2016-06-04 17:52:40 -05:00
# flags for warning on undefined symbols
# TODO figure out why FreeBSD follows linked libraries here
# TODO figure out MSVC equivalents
if ( BUILD_SHARED_LIBS )
if ( NOT ( ${ CMAKE_SYSTEM_NAME } STREQUAL FreeBSD ) )
# on OS X we don't need to do this; Apple's linker warns about undefined symbols in -shared builds!
if ( NOT APPLE )
target_link_libraries ( libui
P R I V A T E - W l , - - n o - u n d e f i n e d - W l , - - n o - a l l o w - s h l i b - u n d e f i n e d
)
endif ( )
endif ( )
endif ( )
2016-06-03 20:14:55 -05:00
endif ( )
if ( BUILD_SHARED_LIBS )
if ( _HASVERSION )
2018-05-12 22:59:43 -05:00
set_target_properties ( libui PROPERTIES
2016-06-03 20:14:55 -05:00
S O V E R S I O N " $ { _ V E R S I O N } " )
endif ( )
endif ( )
2016-06-03 13:56:21 -05:00
macro ( _add_exec _name )
2017-02-22 02:43:43 -06:00
# TODOTODO re-add WIN32 when merging back
2016-06-03 13:56:21 -05:00
add_executable ( ${ _name }
2017-02-22 02:43:43 -06:00
E X C L U D E _ F R O M _ A L L
2016-06-03 13:56:21 -05:00
$ { A R G N } )
2018-05-02 22:04:43 -05:00
target_link_libraries ( ${ _name } libui )
2016-06-03 16:30:00 -05:00
_target_link_options_private ( ${ _name }
_ C O M M O N _ L D F L A G S )
2016-06-03 17:28:14 -05:00
# make shared-linked executables PIC too
if ( BUILD_SHARED_LIBS )
set_property ( TARGET ${ _name } PROPERTY
P O S I T I O N _ I N D E P E N D E N T _ C O D E T r u e )
endif ( )
2016-06-17 09:13:46 -05:00
# TODO see above about INTERFACE
if ( NOT BUILD_SHARED_LIBS )
target_link_libraries ( ${ _name }
2016-06-17 09:16:53 -05:00
$ { _ L I B U I _ L I B S } )
2016-06-17 09:13:46 -05:00
endif ( )
2016-06-04 13:59:26 -05:00
# TODOfor some reason these don't propagate
if ( NOT WIN32 )
target_include_directories ( ${ _name }
P U B L I C . )
target_compile_options ( ${ _name }
P U B L I C $ { _ C O M M O N _ C F L A G S } )
endif ( )
2016-06-03 13:56:21 -05:00
endmacro ( )
2016-06-03 16:30:00 -05:00
add_subdirectory ( "test" )
2016-06-03 18:25:43 -05:00
add_subdirectory ( "examples" )