kwesolowski
kwesolowski

Reputation: 724

Wrapping cmake build with makefile

I have a project with existing makefile based build system, where I want to add existing cmake based component.

What is best way to integrated cmake configure step? As a $(shell ) call evaluating some variable? That runs configure on each invocation...

How to integrate building itself? Just (MAKE) -C cmake-build-dir [targets]?

Upvotes: 4

Views: 2649

Answers (2)

wic
wic

Reputation: 11

In addition to raspy answer I would also add .PHONY to the Makefile target:

.PHONY: $(CMAKE_BUILD_DIR)/Makefile
$(CMAKE_BUILD_DIR)/Makefile: $(CMAKE_SOURCE_DIR)/CMakeLists.txt
    cmake -S $(<D) -B $(@D)

Since the file Makefile is already there, and its dependencies didn't change, nothing will be done when changing the sources unless we treat the Makefile as always out-of-date.

Upvotes: 1

raspy
raspy

Reputation: 4261

The goal of CMake is to generate a Makefile, so I would go with something like this:

CMAKE_BUILD_DIR := some_dir
CMAKE_SOURCE_DIR := some_src_dir

$(CMAKE_BUILD_DIR)/Makefile: $(CMAKE_SOURCE_DIR)/CMakeLists.txt
    cmake -S $(<D) -B $(@D)

.PHONY: $(CMAKE_BUILD_DIR)/built_executable_or_library  # to allow CMake's make check the build
$(CMAKE_BUILD_DIR)/built_executable_or_library: $(CMAKE_BUILD_DIR)/Makefile
    $(MAKE) -C $(@D) $(@F)

This should call CMake configure step when the Makefile does not exist and run make directly to build whatever you need (probably you would need to tailor called targets to your needs). CMake's generated Makefiles check the generated build system itself, so it will be reconfigured as needed.

Upvotes: 4

Related Questions