2016-06-03 13:56:21 -05:00
# 3 june 2016
cmake_minimum_required ( VERSION 2.8.11 )
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-03 20:48:10 -05:00
# the docs say we need to set this up prior to project()
set ( CMAKE_OSX_DEPLOYMENT_TARGET "10.8" )
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-03 13:56:21 -05:00
project ( libui LANGUAGES C CXX )
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
string ( APPEND CMAKE_C_FLAGS " --std=c99" )
string ( APPEND CMAKE_CXX_FLAGS " --std=c++11" )
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-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}" )
add_library ( ${ _LIBUINAME } ${ _LIBUI_SOURCES } )
target_include_directories ( ${ _LIBUINAME }
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 } )
target_compile_definitions ( ${ _LIBUINAME }
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
target_compile_definitions ( ${ _LIBUINAME }
P R I V A T E l i b u i _ E X P O R T S )
2016-06-03 13:56:21 -05:00
target_compile_options ( ${ _LIBUINAME }
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
# because we need 2.8.11 for CentOS, we can't use target_link_libraries(INTERFACE) for static executables :(
if ( BUILD_SHARED_LIBS )
target_link_libraries ( ${ _LIBUINAME }
P R I V A T E $ { _ L I B U I _ L I B S } )
endif ( )
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 )
2016-06-03 16:30:00 -05:00
_target_link_options_private ( ${ _LIBUINAME }
_ 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 )
_handle_static ( )
2016-06-04 12:57:10 -05:00
# TODO this really should be PRIVATE but I haven't fully figured this out
2016-06-03 13:56:21 -05:00
target_compile_definitions ( ${ _LIBUINAME }
2016-06-04 12:57:10 -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 )
endif ( )
if ( BUILD_SHARED_LIBS )
if ( _HASVERSION )
set_target_properties ( ${ _LIBUINAME } PROPERTIES
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 )
add_executable ( ${ _name }
WIN32 E X C L U D E _ F R O M _ A L L
$ { A R G N } )
2016-06-04 12:57:10 -05:00
target_link_libraries ( ${ _name } libui ${ _LIBUI_STATIC_RES } )
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 ( )
# because we need 2.8.11 for CentOS, we can't use target_link_libraries(PUBLIC) for static executables :(
if ( NOT BUILD_SHARED_LIBS )
target_link_libraries ( ${ _name } ${ _LIBUI_LIBS } )
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" )