Cmake

From campisano.org
Jump to navigation Jump to search

Howto CMake conf compatible with make

Example

Tree project, Base, Infrastructure, Test.


  • Common Makefile facade to CMakefile:
mkdir config
nano config/Makefile.inc

use Makefile for CMake


Base simple lib

mkdir Base Base/base
nano Base/CMakeLists.txt
cmake_minimum_required(VERSION 2.6)

set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-Wall -pipe")

set(var_target CommonBase)
set(var_path_source base)
project(Prj_${var_target})

file(GLOB_RECURSE var_sources ${var_path_source}/*.cpp)
add_library(${var_target} SHARED ${var_sources})

install(TARGETS ${var_target} DESTINATION ${PROJECT_SOURCE_DIR}/install)
cat > Base/Makefile << EOF
include ../config/Makefile.inc
EOF


Infrastructure middle library

mkdir Infrastructure Infrastructure/infrastructure
nano Infrastructure/CMakeLists.txt
cmake_minimum_required(VERSION 2.6)

set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-Wall -pipe")

set(var_target CommonInfrastructure)
set(var_path_source infrastructure)
project(Prj_${var_target})

include_directories(../Base /usr/include/apr-1.0)
link_directories(${PROJECT_SOURCE_DIR}/../Base/build/${CMAKE_BUILD_TYPE} /usr/lib)

file(GLOB_RECURSE var_sources ${var_path_source}/*.cpp)
add_library(${var_target} SHARED ${var_sources})
target_link_libraries(${var_target} CommonBase apr-1 aprutil-1)

install(TARGETS ${var_target} DESTINATION ${PROJECT_SOURCE_DIR}/install)
cat > Infrastructure/Makefile << EOF
include ../config/Makefile.inc
EOF


Test executable

mkdir Test Test/test
nano Test/CMakeLists.txt
cmake_minimum_required(VERSION 2.6)

set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-Wall -pipe")

set(var_target Test)
set(var_path_source test)
project(Prj_${var_target})

include_directories(../Base ../Infrastructure)
link_directories(${PROJECT_SOURCE_DIR}/../Base/build/${CMAKE_BUILD_TYPE}
                 ${PROJECT_SOURCE_DIR}/../Infrastructure/build/${CMAKE_BUILD_TYPE})

file(GLOB_RECURSE var_sources ${var_path_source}/*.cpp)
add_executable(${var_target} ${var_sources})
target_link_libraries(${var_target} CommonBase CommonInfrastructure)

install(TARGETS ${var_target} DESTINATION ${PROJECT_SOURCE_DIR}/install)
cat > Test/Makefile << EOF
include ../config/Makefile.inc
EOF


Global Makefile to build all and run tests

nano Makefile


# Makefile
#



# make options
MAKEFLAGS+= -s

SUBDIRS = Base Infrastructure Test
RUN = Test



all:
	@for dir in $(SUBDIRS); do \
	$(MAKE) -C $$dir $(MAKECMDGOALS) || exit; \
	done;

release: all

debug: all

install: all
	test -d install/ || mkdir install/;
	@for dir in $(SUBDIRS); do \
	cp -a $$dir/install/* install/
	done;

install_debug: all
	test -d install/ || mkdir install/;
	@for dir in $(SUBDIRS); do \
	cp -a $$dir/install/* install/; \
	done;

clean: all

purge: all

run:
	test -d install/ || exit;
	test -x install/$(RUN) || exit;
	LD_LIBRARY_PATH=./install/ exec ./install/$(RUN);



# End


Links