cmake.mk 5.36 KB
Newer Older
makc's avatar
makc committed
1
2
3
4
5
6
# $FreeBSD$
#
# Provide support for CMake based projects
#
# Feature:		cmake
# Usage:		USES=cmake or USES=cmake:ARGS
adridg's avatar
adridg committed
7
# Valid ARGS:		insource, run, noninja, testing
makc's avatar
makc committed
8
# ARGS description:
9
# insource		do not perform an out-of-source build
tcberner's avatar
tcberner committed
10
11
12
13
14
15
16
17
18
# noninja		don't use ninja instead of make
#			Setting this should be an exception, and hints to an issue
#			inside the ports build system.
#			A few corner cases never use ninja, and are handled, to reduce
#			the usage of 'noninja'.:
#				1) fortran ports
#				2) ports that set BUILD_- or INSTALL_WRKSRC to
#				   something different than CONFIGURE_WRKSRC
# run			add a runtime dependency on cmake
adridg's avatar
adridg committed
19
20
21
# testing		add the test target based on ctest
#			Additionally, CMAKE_TESTING_ON, CMAKE_TESTING_OFF, CMAKE_TESTING_ARGS, CMAKE_TESTING_TARGET
#			can be defined to override the default values.
makc's avatar
makc committed
22
23
24
25
26
27
#
#
# Additional variables that affect cmake behaviour:
#
# User defined variables:
# CMAKE_NOCOLOR		- Disable colour build output
28
#			Default: not set, unless BATCH or PACKAGE_BUILDING is defined
makc's avatar
makc committed
29
30
#
# Variables for ports:
31
32
# CMAKE_ON		Appends -D<var>:bool=ON  to the CMAKE_ARGS,
# CMAKE_OFF		Appends -D<var>:bool=OFF to the CMAKE_ARGS.
makc's avatar
makc committed
33
34
35
36
37
38
39
40
41
42
43
44
45
# CMAKE_ARGS		- Arguments passed to cmake
#			Default: see below
# CMAKE_BUILD_TYPE	- Type of build (cmake predefined build types).
#			Projects may have their own build profiles.
#			CMake supports the following types: Debug,
#			Release, RelWithDebInfo and MinSizeRel.
#			Debug and Release profiles respect system
#			CFLAGS, RelWithDebInfo and MinSizeRel will set
#			CFLAGS to "-O2 -g" and "-Os -DNDEBUG".
#			Default: Release, if WITH_DEBUG is not set,
#			Debug otherwise
# CMAKE_SOURCE_PATH	- Path to the source directory
#			Default: ${WRKSRC}
46
47
#
# MAINTAINER: kde@FreeBSD.org
makc's avatar
makc committed
48
49
50
51

.if !defined(_INCLUDE_USES_CMAKE_MK)
_INCLUDE_USES_CMAKE_MK=	yes

adridg's avatar
adridg committed
52
_valid_ARGS=		insource run noninja testing
makc's avatar
makc committed
53
54

# Sanity check
tijl's avatar
tijl committed
55
.for arg in ${cmake_ARGS}
makc's avatar
makc committed
56
57
58
.    if empty(_valid_ARGS:M${arg})
IGNORE=	Incorrect 'USES+= cmake:${cmake_ARGS}' usage: argument [${arg}] is not recognized
.    endif
tijl's avatar
tijl committed
59
.endfor
makc's avatar
makc committed
60
61

CMAKE_BIN=		${LOCALBASE}/bin/cmake
62
BUILD_DEPENDS+=		${CMAKE_BIN}:devel/cmake
makc's avatar
makc committed
63

tijl's avatar
tijl committed
64
.if ${cmake_ARGS:Mrun}
65
RUN_DEPENDS+=		${CMAKE_BIN}:devel/cmake
makc's avatar
makc committed
66
67
68
69
70
71
72
73
.endif

.if defined(WITH_DEBUG)
CMAKE_BUILD_TYPE?=	Debug
.else
CMAKE_BUILD_TYPE?=	Release
.endif #defined(WITH_DEBUG)

bapt's avatar
bapt committed
74
PLIST_SUB+=		CMAKE_BUILD_TYPE="${CMAKE_BUILD_TYPE:tl}"
makc's avatar
makc committed
75

76
.if defined(STRIP) && ${STRIP} != "" && !defined(WITH_DEBUG)
makc's avatar
makc committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
INSTALL_TARGET?=	install/strip
.endif

CMAKE_ARGS+=		-DCMAKE_C_COMPILER:STRING="${CC}" \
			-DCMAKE_CXX_COMPILER:STRING="${CXX}" \
			-DCMAKE_C_FLAGS:STRING="${CFLAGS}" \
			-DCMAKE_C_FLAGS_DEBUG:STRING="${CFLAGS}" \
			-DCMAKE_C_FLAGS_RELEASE:STRING="${CFLAGS}" \
			-DCMAKE_CXX_FLAGS:STRING="${CXXFLAGS}" \
			-DCMAKE_CXX_FLAGS_DEBUG:STRING="${CXXFLAGS}" \
			-DCMAKE_CXX_FLAGS_RELEASE:STRING="${CXXFLAGS}" \
			-DCMAKE_EXE_LINKER_FLAGS:STRING="${LDFLAGS}" \
			-DCMAKE_MODULE_LINKER_FLAGS:STRING="${LDFLAGS}" \
			-DCMAKE_SHARED_LINKER_FLAGS:STRING="${LDFLAGS}" \
91
			-DCMAKE_INSTALL_PREFIX:PATH="${CMAKE_INSTALL_PREFIX}" \
makc's avatar
makc committed
92
			-DCMAKE_BUILD_TYPE:STRING="${CMAKE_BUILD_TYPE}" \
93
			-DTHREADS_HAVE_PTHREAD_ARG:BOOL=YES \
94
95
			-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=YES \
			-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON
makc's avatar
makc committed
96

97
98
99
100
101
102
103
# Handle the option-like CMAKE_ON and CMAKE_OFF lists.
.for _bool_kind in ON OFF
.  if defined(CMAKE_${_bool_kind})
CMAKE_ARGS+=		${CMAKE_${_bool_kind}:C/.*/-D&:BOOL=${_bool_kind}/}
.  endif
.endfor

104
105
CMAKE_INSTALL_PREFIX?=	${PREFIX}

makc's avatar
makc committed
106
107
108
109
110
111
112
113
114
115
116
.if defined(BATCH) || defined(PACKAGE_BUILDING)
CMAKE_NOCOLOR=		yes
.endif

.if defined(CMAKE_NOCOLOR)
CMAKE_ARGS+=		-DCMAKE_COLOR_MAKEFILE:BOOL=OFF
.endif

_CMAKE_MSG=		"===>  Performing in-source build"
CMAKE_SOURCE_PATH?=	${WRKSRC}

117
.if empty(cmake_ARGS:Minsource)
makc's avatar
makc committed
118
119
_CMAKE_MSG=		"===>  Performing out-of-source build"
CONFIGURE_WRKSRC=	${WRKDIR}/.build
120
121
BUILD_WRKSRC?=		${CONFIGURE_WRKSRC}
INSTALL_WRKSRC?=	${CONFIGURE_WRKSRC}
122
TEST_WRKSRC?=		${CONFIGURE_WRKSRC}
makc's avatar
makc committed
123
124
.endif

tcberner's avatar
tcberner committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# By default we use the ninja generator.
#  Except, if cmake:run is set (cmake not wanted as generator)
#             fortran is used, as the ninja-generator does not handle it.
#             or if CONFIGURE_WRKSRC does not match  BUILD_WRKSRC or INSTALL_WRKSRC
#             as the build.ninja file won't be where ninja expects it.
.if empty(cmake_ARGS:Mnoninja) && empty(cmake_ARGS:Mrun) && empty(USES:Mfortran)
.  if "${CONFIGURE_WRKSRC}" == "${BUILD_WRKSRC}" && "${CONFIGURE_WRKSRC}" == "${INSTALL_WRKSRC}"
.    if ! empty(USES:Mgmake)
BROKEN=		USES=gmake is incompatible with cmake's ninja-generator
.    endif
.      include "${USESDIR}/ninja.mk"
.  endif
.endif

makc's avatar
makc committed
139
140
141
142
.if !target(do-configure)
do-configure:
	@${ECHO_MSG} ${_CMAKE_MSG}
	${MKDIR} ${CONFIGURE_WRKSRC}
143
	@cd ${CONFIGURE_WRKSRC}; ${SETENV} ${CONFIGURE_ENV} ${CMAKE_BIN} ${CMAKE_ARGS} ${CMAKE_SOURCE_PATH}
makc's avatar
makc committed
144
145
.endif

adridg's avatar
adridg committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
.if !target(do-test) && ${cmake_ARGS:Mtesting}
CMAKE_TESTING_ON?=		BUILD_TESTING
CMAKE_TESTING_TARGET?=		test

# Handle the option-like CMAKE_TESTING_ON and CMAKE_TESTING_OFF lists.
.for _bool_kind in ON OFF
.  if defined(CMAKE_TESTING_${_bool_kind})
CMAKE_TESTING_ARGS+=		${CMAKE_TESTING_${_bool_kind}:C/.*/-D&:BOOL=${_bool_kind}/}
.  endif
.endfor

do-test:
	@cd ${BUILD_WRKSRC} && \
		${SETENV} ${CONFIGURE_ENV} ${CMAKE_BIN} ${CMAKE_ARGS} ${CMAKE_TESTING_ARGS} ${CMAKE_SOURCE_PATH} && \
		${SETENV} ${MAKE_ENV} ${MAKE_CMD} ${MAKE_ARGS} ${ALL_TARGET} && \
		${SETENV} ${MAKE_ENV} ${MAKE_CMD} ${MAKE_ARGS} ${CMAKE_TESTING_TARGET}
.endif

makc's avatar
makc committed
164
.endif #!defined(_INCLUDE_USES_CMAKE_MK)