From 24d37efa5dae04fbe143c33b8c6710a8606c5451 Mon Sep 17 00:00:00 2001 From: Tony Theodore Date: Sat, 4 Feb 2017 17:58:04 +1100 Subject: cmake: improve configuration with templates - move cmake configuration from mxe-conf to cmake-conf - replace `echo` with templates for readability and maintenance - allow packages to set other dep files - set CMAKE_POLICY_DEFAULT_CMPNNNN in wrapper since `cmake_minimum_required` or `cmake_policy` can't be set in toolchain (closes #971) --- Makefile | 3 +- src/cmake-conf.mk | 57 ++++++++++++++++++++++++++++++++++ src/cmake/conf/mxe-conf.cmake.in | 67 ++++++++++++++++++++++++++++++++++++++++ src/cmake/conf/target-cmake.in | 25 +++++++++++++++ src/mxe-conf.mk | 64 -------------------------------------- 5 files changed, 151 insertions(+), 65 deletions(-) create mode 100644 src/cmake-conf.mk create mode 100644 src/cmake/conf/mxe-conf.cmake.in create mode 100644 src/cmake/conf/target-cmake.in diff --git a/Makefile b/Makefile index 1364ebd..0e8d56a 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,7 @@ STRIP_EXE := $(true) # All pkgs have (implied) order-only dependencies on MXE_CONF_PKGS. # These aren't meaningful to the pkg list in http://mxe.cc/#packages so # use a list in case we want to separate autotools, cmake etc. -MXE_CONF_PKGS := mxe-conf +MXE_CONF_PKGS := cmake-conf mxe-conf # define some whitespace variables define newline @@ -525,6 +525,7 @@ $(1): $(PREFIX)/$(3)/installed/$(1) $(PREFIX)/$(3)/installed/$(1): $(PKG_MAKEFILES) \ $(PKG_PATCHES) \ $(PKG_TESTFILES) \ + $($(1)_FILE_DEPS) \ $(addprefix $(PREFIX)/$(3)/installed/,$(value $(call LOOKUP_PKG_RULE,$(1),DEPS,$(3)))) \ $(and $($(3)_DEPS),$(addprefix $(PREFIX)/$($(3)_DEPS)/installed/,$(filter-out $(MXE_CONF_PKGS),$($($(3)_DEPS)_PKGS)))) \ | $(if $(DONT_CHECK_REQUIREMENTS),,check-requirements) \ diff --git a/src/cmake-conf.mk b/src/cmake-conf.mk new file mode 100644 index 0000000..d9c086d --- /dev/null +++ b/src/cmake-conf.mk @@ -0,0 +1,57 @@ +# This file is part of MXE. See LICENSE.md for licensing information. + +PKG := cmake-conf +$(PKG)_VERSION := 1 +$(PKG)_UPDATE := echo 1 +$(PKG)_TARGETS := $(BUILD) $(MXE_TARGETS) +$(PKG)_FILE_DEPS := $(wildcard $(PWD)/src/cmake/conf/*) + +define $(PKG)_BUILD + # create the CMake toolchain file using template + # individual packages (e.g. hdf5) should add their + # own files under CMAKE_TOOLCHAIN_DIR + + mkdir -p '$(CMAKE_TOOLCHAIN_DIR)' + touch '$(CMAKE_TOOLCHAIN_DIR)/.gitkeep' + cmake-configure-file \ + -DCMAKE_VERSION=$(cmake_VERSION) \ + -DCMAKE_SHARED_BOOL=$(CMAKE_SHARED_BOOL) \ + -DCMAKE_STATIC_BOOL=$(CMAKE_STATIC_BOOL) \ + -DLIBTYPE=$(if $(BUILD_SHARED),SHARED,STATIC) \ + -DPREFIX=$(PREFIX) \ + -DTARGET=$(TARGET) \ + -DBUILD=$(BUILD) \ + -DCMAKE_TOOLCHAIN_DIR='$(CMAKE_TOOLCHAIN_DIR)' \ + -DINPUT='$(PWD)/src/cmake/conf/mxe-conf.cmake.in' \ + -DOUTPUT='$(CMAKE_TOOLCHAIN_FILE)' + + #create prefixed cmake wrapper script + cmake-configure-file \ + -DCMAKE_VERSION=$(cmake_VERSION) \ + -DPREFIX=$(PREFIX) \ + -DTARGET=$(TARGET) \ + -DBUILD=$(BUILD) \ + -DCMAKE_TOOLCHAIN_FILE='$(CMAKE_TOOLCHAIN_FILE)' \ + -DCMAKE_RUNRESULT_FILE='$(CMAKE_RUNRESULT_FILE)' \ + -DINPUT='$(PWD)/src/cmake/conf/target-cmake.in' \ + -DOUTPUT='$(PREFIX)/bin/$(TARGET)-cmake' + chmod 0755 '$(PREFIX)/bin/$(TARGET)-cmake' +endef + +define $(PKG)_BUILD_$(BUILD) + # install cmake modules + mkdir -p '$(PREFIX)/share/cmake/modules' + $(INSTALL) -m644 '$(PWD)/src/cmake/modules/'* '$(PREFIX)/share/cmake/modules' + + # install cmake-configure-file for general use + # cmake-configure-file -DINPUT -DOUTPUT -DFOO -DBAR -D... + mkdir -p '$(PREFIX)/bin' + echo 'configure_file($${INPUT} $${OUTPUT} @ONLY)' \ + > '$(PREFIX)/share/cmake/modules/configure_file.cmake' + (echo '#!/usr/bin/env bash'; \ + echo 'exec "$(PREFIX)/$(BUILD)/bin/cmake" "$$@" \ + -P "$(PREFIX)/share/cmake/modules/configure_file.cmake"'; \ + ) \ + > '$(PREFIX)/bin/cmake-configure-file' + chmod 0755 '$(PREFIX)/bin/cmake-configure-file' +endef diff --git a/src/cmake/conf/mxe-conf.cmake.in b/src/cmake/conf/mxe-conf.cmake.in new file mode 100644 index 0000000..d9c6a44 --- /dev/null +++ b/src/cmake/conf/mxe-conf.cmake.in @@ -0,0 +1,67 @@ +# This file is part of MXE. See LICENSE.md for licensing information. + +# https://cmake.org/cmake/help/latest + +# Can't set `cmake_minimum_required` or `cmake_policy` in toolchain +# since toolchain is read before CMakeLists.txt +# See `target-cmake.in` for CMAKE_POLICY_DEFAULT_CMPNNNN + +# Check if we are using mxe supplied version +# - toolchain is included multiple times so set a guard in +# environment to suppress duplicate messages +if(NOT ${CMAKE_COMMAND} STREQUAL @PREFIX@/@BUILD@/bin/cmake AND NOT DEFINED ENV{_MXE_CMAKE_TOOLCHAIN_INCLUDED}) + message(WARNING " +** Warning: direct use of toolchain file is deprecated +** Please use prefixed wrapper script instead: + @TARGET@-cmake [options] + - uses mxe supplied cmake version @CMAKE_VERSION@ + - loads toolchain + - loads common run results + - sets various policy defaults + ") + set(ENV{_MXE_CMAKE_TOOLCHAIN_INCLUDED} TRUE) +endif() + +## General configuration +set(CMAKE_SYSTEM_NAME Windows) +set(MSYS 1) +set(CMAKE_BUILD_TYPE Release CACHE STRING "Debug|Release|RelWithDebInfo|MinSizeRel" FORCE) +set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON) +# Workaround for https://www.cmake.org/Bug/view.php?id=14075 +set(CMAKE_CROSS_COMPILING ON) + + +## Library config +set(BUILD_SHARED_LIBS @CMAKE_SHARED_BOOL@ CACHE BOOL "BUILD_SHARED_LIBS" FORCE) +set(BUILD_STATIC_LIBS @CMAKE_STATIC_BOOL@ CACHE BOOL "BUILD_STATIC_LIBS" FORCE) +set(BUILD_SHARED @CMAKE_SHARED_BOOL@ CACHE BOOL "BUILD_SHARED" FORCE) +set(BUILD_STATIC @CMAKE_STATIC_BOOL@ CACHE BOOL "BUILD_STATIC" FORCE) +set(LIBTYPE @LIBTYPE@) + + +## Paths etc. +set(CMAKE_FIND_ROOT_PATH @PREFIX@/@TARGET@) +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_PREFIX_PATH @PREFIX@/@TARGET@) +set(CMAKE_INSTALL_PREFIX @PREFIX@/@TARGET@ CACHE PATH "Installation Prefix") +# For custom mxe FindPackage scripts +set(CMAKE_MODULE_PATH "@PREFIX@/share/cmake/modules" ${CMAKE_MODULE_PATH}) + + +## Programs +set(CMAKE_C_COMPILER @PREFIX@/bin/@TARGET@-gcc) +set(CMAKE_CXX_COMPILER @PREFIX@/bin/@TARGET@-g++) +set(CMAKE_Fortran_COMPILER @PREFIX@/bin/@TARGET@-gfortran) +set(CMAKE_RC_COMPILER @PREFIX@/bin/@TARGET@-windres) +# CMAKE_RC_COMPILE_OBJECT is defined in: +# /share/cmake-X.Y/Modules/Platform/Windows-windres.cmake + +## Individual package configuration +file(GLOB mxe_cmake_files + "@CMAKE_TOOLCHAIN_DIR@/*.cmake" +) +foreach(mxe_cmake_file ${mxe_cmake_files}) + include(${mxe_cmake_file}) +endforeach() diff --git a/src/cmake/conf/target-cmake.in b/src/cmake/conf/target-cmake.in new file mode 100644 index 0000000..5858a7b --- /dev/null +++ b/src/cmake/conf/target-cmake.in @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +echo "== Using MXE wrapper: @PREFIX@/bin/@TARGET@-cmake" + +# https://cmake.org/cmake/help/latest/manual/cmake-policies.7.html +# https://cmake.org/cmake/help/latest/variable/CMAKE_POLICY_DEFAULT_CMPNNNN.html +POLICIES=(0017,0020) + +unset NO_MXE_TOOLCHAIN +if echo -- "$@" | grep -Ewq "(--build|-E|--system-information)" ; then + NO_MXE_TOOLCHAIN=1 +fi +if [[ "$NO_MXE_TOOLCHAIN" == "1" ]]; then + echo "== Skip using MXE toolchain: @CMAKE_TOOLCHAIN_FILE@" + # see https://github.com/mxe/mxe/issues/932 + exec "@PREFIX@/@BUILD@/bin/cmake" "$@" +else + echo " - cmake version @CMAKE_VERSION@" + echo " - warnings for unused CMAKE_POLICY_DEFAULT variables can be ignored" + echo "== Using MXE toolchain: @CMAKE_TOOLCHAIN_FILE@" + echo "== Using MXE runresult: @CMAKE_RUNRESULT_FILE@" + exec "@PREFIX@/@BUILD@/bin/cmake" \ + -DCMAKE_TOOLCHAIN_FILE="@CMAKE_TOOLCHAIN_FILE@" \ + `eval echo -DCMAKE_POLICY_DEFAULT_CMP{$POLICIES}=NEW` \ + -C"@CMAKE_RUNRESULT_FILE@" "$@" +fi diff --git a/src/mxe-conf.mk b/src/mxe-conf.mk index a95a1ed..ef67b47 100644 --- a/src/mxe-conf.mk +++ b/src/mxe-conf.mk @@ -16,66 +16,6 @@ define $(PKG)_BUILD # setting ac_cv_build bypasses the config.guess check in every package echo "ac_cv_build=$(BUILD)" > '$(PREFIX)/$(TARGET)/share/config.site' - # create the CMake toolchain file - # individual packages (e.g. hdf5) should add their - # own files under CMAKE_TOOLCHAIN_DIR - # CMAKE_RC_COMPILE_OBJECT is defined in: - # /share/cmake-X.Y/Modules/Platform/Windows-windres.cmake - - mkdir -p '$(CMAKE_TOOLCHAIN_DIR)' - touch '$(CMAKE_TOOLCHAIN_DIR)/.gitkeep' - (echo 'set(CMAKE_SYSTEM_NAME Windows)'; \ - echo 'set(MSYS 1)'; \ - echo 'set(BUILD_SHARED_LIBS $(CMAKE_SHARED_BOOL) CACHE BOOL "BUILD_SHARED_LIBS" FORCE)'; \ - echo 'set(BUILD_STATIC_LIBS $(CMAKE_STATIC_BOOL) CACHE BOOL "BUILD_STATIC_LIBS" FORCE)'; \ - echo 'set(BUILD_SHARED $(CMAKE_SHARED_BOOL) CACHE BOOL "BUILD_SHARED" FORCE)'; \ - echo 'set(BUILD_STATIC $(CMAKE_STATIC_BOOL) CACHE BOOL "BUILD_STATIC" FORCE)'; \ - echo 'set(LIBTYPE $(if $(BUILD_SHARED),SHARED,STATIC))'; \ - echo 'set(CMAKE_EXPORT_NO_PACKAGE_REGISTRY ON)'; \ - echo 'set(CMAKE_PREFIX_PATH $(PREFIX)/$(TARGET))'; \ - echo 'set(CMAKE_FIND_ROOT_PATH $(PREFIX)/$(TARGET))'; \ - echo 'set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)'; \ - echo 'set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)'; \ - echo 'set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)'; \ - echo 'set(CMAKE_C_COMPILER $(PREFIX)/bin/$(TARGET)-gcc)'; \ - echo 'set(CMAKE_CXX_COMPILER $(PREFIX)/bin/$(TARGET)-g++)'; \ - echo 'set(CMAKE_Fortran_COMPILER $(PREFIX)/bin/$(TARGET)-gfortran)'; \ - echo 'set(CMAKE_RC_COMPILER $(PREFIX)/bin/$(TARGET)-windres)'; \ - echo 'set(CMAKE_MODULE_PATH "$(PREFIX)/share/cmake/modules" $${CMAKE_MODULE_PATH}) # For mxe FindPackage scripts'; \ - echo 'set(CMAKE_INSTALL_PREFIX $(PREFIX)/$(TARGET) CACHE PATH "Installation Prefix")'; \ - echo 'set(CMAKE_BUILD_TYPE Release CACHE STRING "Debug|Release|RelWithDebInfo|MinSizeRel" FORCE)'; \ - echo 'set(CMAKE_CROSS_COMPILING ON) # Workaround for https://www.cmake.org/Bug/view.php?id=14075'; \ - echo ''; \ - echo 'file(GLOB mxe_cmake_files'; \ - echo ' "$(CMAKE_TOOLCHAIN_DIR)/*.cmake"'; \ - echo ')'; \ - echo 'foreach(mxe_cmake_file $${mxe_cmake_files})'; \ - echo ' include($${mxe_cmake_file})'; \ - echo 'endforeach()'; \ - ) > '$(CMAKE_TOOLCHAIN_FILE)' - - #create prefixed cmake wrapper script - (echo '#!/usr/bin/env bash'; \ - echo 'echo "== Using MXE wrapper: $(PREFIX)/bin/$(TARGET)-cmake"'; \ - echo 'unset NO_MXE_TOOLCHAIN'; \ - echo 'if echo -- "$$@" | grep -Ewq "(--build|-E|--system-information)" ; then'; \ - echo ' NO_MXE_TOOLCHAIN=1'; \ - echo 'fi'; \ - echo 'if [[ "$$NO_MXE_TOOLCHAIN" == "1" ]]; then'; \ - echo ' echo "== Skip using MXE toolchain: $(CMAKE_TOOLCHAIN_FILE)"'; \ - echo ' # see https://github.com/mxe/mxe/issues/932'; \ - echo ' exec "$(PREFIX)/$(BUILD)/bin/cmake" "$$@"'; \ - echo 'else'; \ - echo ' echo "== Using MXE toolchain: $(CMAKE_TOOLCHAIN_FILE)"'; \ - echo ' echo "== Using MXE runresult: $(CMAKE_RUNRESULT_FILE)"'; \ - echo ' exec "$(PREFIX)/$(BUILD)/bin/cmake" \ - -DCMAKE_TOOLCHAIN_FILE="$(CMAKE_TOOLCHAIN_FILE)" \ - -C"$(CMAKE_RUNRESULT_FILE)" "$$@"'; \ - echo 'fi'; \ - ) \ - > '$(PREFIX)/bin/$(TARGET)-cmake' - chmod 0755 '$(PREFIX)/bin/$(TARGET)-cmake' - # create pkg-config files for OpenGL/GLU mkdir -p '$(PREFIX)/$(TARGET)/lib/pkgconfig' (echo 'Name: gl'; \ @@ -96,10 +36,6 @@ define $(PKG)_BUILD_$(BUILD) mkdir -p '$(PREFIX)/bin' $(INSTALL) -m755 '$(EXT_DIR)/config.guess' '$(PREFIX)/bin/' - # install cmake modules - mkdir -p '$(PREFIX)/share/cmake/modules' - $(INSTALL) -m644 '$(PWD)/src/cmake/modules/'* '$(PREFIX)/share/cmake/modules' - # fail early if autotools can't autoreconf # 1. detect mismatches in installation locations # 2. ??? -- cgit v0.12