Git Product home page Git Product logo

cmake-modules's Introduction

Rylie's CMake Modules Collection

Rylie Pavlik, Ph.D.

[email protected]

https://ryliepavlik.com/

Introduction

This is a collection of CMake modules that I've produced during the course of a variety of software development. There are a number of find modules, especially for virtual reality and physical simulation packages, some utility modules of more general interest, and some patches or workarounds for (very old versions of) CMake itself.

Each module is generally documented, and depending on how busy I was when I created it, the documentation can be fairly complete.

By now, it also includes contributions both from open-source projects I work on, as well as friendly strangers on the Internet contributing their modules. I am very grateful for improvements/fixes/pull requests! As usual, contributions to files are assumed to be under the same license as they were offered to you (incoming == outgoing), and any new files must have proper copyright and SPDX license identifer headers.

How to Integrate

How would you like to use these? Some of the modules, particularly older ones, have a number of internal dependencies, and thus would be best placed wholesale into a cmake subdirectory of your project source. Otherwise, you may consider just picking the subset you prefer into such a cmake subdirectory.

All Modules

If you use Git, try installing git-subtree (included by default on Git for Windows and perhaps for your Linux distro, especially post-1.9.1), so you can easily use this repository for subtree merges, updating simply.

For the initial checkout:

cd yourprojectdir

git subtree add --squash --prefix=cmake https://github.com/rpavlik/cmake-modules.git main

For updates:

cd yourprojectdir

git subtree pull --squash --prefix=cmake https://github.com/rpavlik/cmake-modules.git main

If you originally installed this by just copying the files, you'll sadly have to delete the directory, commit that, then do the git subtree add. Annoying, but I don't know a workaround. (Alternately you can use the method described below, for the "subset of modules", to update.)

If you use some other version control, you can export a copy of this directory without the git metadata by calling:

./export-to-directory.sh ../yourprojectdir/cmake

You might also consider exporting to a temp directory and merging changes, since this will not overwrite by default. You can pass -f to overwrite existing files.

Just a few modules

Many newer modules don't have any or many internal dependencies. You can review them to look for any include( statements or other things that increase the files used (e.g. configure_file(, file(READ, file(GENERATE), and make sure you copy those too.

If this is how you originally started using these modules, then running the following from within a clone of this repo will automatically update any files. Be sure you have committed any local changes in your project first to avoid potentially losing work!

./update-modules.sh ../yourprojectdir/cmake/

Note that the script is not smart enough to know about changed dependent scripts/files, nor about scripts with matching names but not originating in this project (it just looks at file names/paths), so manually review the diff before committing in your project.

How to Use

At the minimum, all you have to do is add a line like this near the top of your root CMakeLists.txt file (but not before your project() call):

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

You might also want the extra automatic features/fixes included with the modules, for that, just add another line following the first one:

include(UseBackportedModules)

For more information on individual modules, look at the files themselves: they should all start with a comment.

Licenses

The modules that I wrote myself are all subject to this license:

Copyright 2009-2014, Iowa State University. or Copyright 2014-2017, Sensics, Inc., or Copyright 2018-2023, Collabora, Ltd., or Copyright 2009-2023, Rylie Pavlik

Distributed under the Boost Software License, Version 1.0.

(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Modules based on those included with CMake are under the OSI-approved BSD license, which is included in each of those modules. A few other modules are modified from other sources - when in doubt, look at the .cmake file - each file has correct licensing information.

If you'd like to contribute, that would be great! Just make sure to include the license boilerplate in your module, and send a pull request.

Important License Note

If you find this file inside of another project, rather at the top-level directory, you're in a separate project that is making use of these modules. That separate project can (and probably does) have its own license specifics.

cmake-modules's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cmake-modules's Issues

GetGitRevisionDescription error on old CMake (2.6) since 5fa68c1

I'm having trouble with the changes from rev 5fa68c1 on a system running cmake 2.6-patch 4 (RHEL 6.4). A more recent cmake 2.8.11 works fine. The error message is:

CMake Error at cmake-modules/GetGitRevisionDescription.cmake:47 (get_filename_component):
  get_filename_component called with incorrect number of arguments

get_git_head_revision in detached mode

Hi,

The tag is not correctly propagated if the tag is in a detached mode.
This is basically the same issue as the issue below, but instead of a branch the information needed is a tag.
#3

You can recreate the issue by creating a tag on a different branch than master, for example, the branch should not be checkout, but you directly check out the tag.

Cheers
Alina

`get_git_head_revision` forces full project recompile on every different commit

Description

Issue present in 77982ba

  • cmake version 3.22.2
  • Linux (Arch Distro)
  • MakeFile Generator

We're using get_git_head_revision in the following way:

# Include git hash in source
include(CMakeModules/GetGitRevisionDescription.cmake)
get_git_head_revision(GIT_REFSPEC GIT_SHA1)
message(STATUS "Building ${CMAKE_PROJECT_NAME} GIT SHA1: ${GIT_SHA1}")

set_property(
  SOURCE ${CMAKE_CURRENT_LIST_DIR}/GitSHA1.cpp
  PROPERTY COMPILE_DEFINITIONS
    "GIT_SHA1=\"${GIT_SHA1}\""
)

It seems like for every different commit, instead of only recompiling changes + GitSHA1.cpp, our entire project is recompiled.

This makes it very annoying when doing things such as git bisect or rebases, or simply making new commits. It also wastes a lot of cycles.

How to reproduce

  1. Any project making use of get_git_head_revision in a way similar to the above snippet
  2. Compile the project once
  3. Compile it again: there should be nothing new to compile
  4. git commit --allow-empy -m "New SHA, no changes"

Current behaviour

Project reconfigures and will recompile every source file in project

Expected behaviour

Compiling the project once more should be like 3. above, except for only the files related to the SHA define recompiling

FindNSS and FindNSPR?

Hi @rpavlik

Would you be interested in adding FindNSS and FindNSPR to your repo?

This repo helps loads, and it would be great to add scripts we have separately to it - they are both BSD-Clause-3 licensed, originally by Andreas Schneider at RedHat.

get_git_head_revision() fails due to unfound git

I was seeing an error like this (anonymized):

CMake Error: File XXX/MyProject/SubmodulesInThisDir/.git/modules/SubmodulesInThisDir/Submodule/HEAD does not exist.
CMake Error at SubmodulesInThisDir/Submodule/cmake/GetGitRevisionDescription.cmake:161 (configure_file):
  configure_file Problem configuring file

Finally tracked it down to the fact that, for some reason, the get_git_head_revision() function of GetGitRevisionDescription does not include the

    if(NOT GIT_FOUND)
        find_package(Git QUIET)
    endif()

block that all the other public functions have. So, if this is the first GetGitRevisionDescription function you call, it takes the wrong code paths due to the inability to call Git and tries to configure the wrong file.

git_describe() and --dirty

It seems that git_describe() can't be used with --dirty - it will always return -128-NOTFOUND. When running the same command from the command line yields this:

$git describe HEAD --dirty
fatal: --dirty is incompatible with commit-ishes

So basically git doesn't like that we're passing both hash and --dirty. I don't see why we have to pass commit hash to git describe in git_describe(), by default git will just assume HEAD.

Option to prevent get_git_head_revision from traversing parent directories

There's a patch here. They mentioned upstreaming it but never got around to it.

To summarize, if you are versioning your home directory with git and unpack a source tarball then the CMake module will still traverse upwards and use the revision of the home directory. Most projects use a CMakeLists.txt in the root directory, so the traversal is not needed, and disabling it solves the issue.

update-modules.sh should use same path for file check and copy

if [ -f "${relative}" ]; then

Should probably read:

    # Look through nearly every file in the cmake-modules repo, but don't copy this file to your project!
    (cd $MODULES_DIR && git ls-files | grep -v "update-modules") | while read -r relative; do
        if [ -f "${MODULES_DIR}/${relative}" ]; then
            echo "${relative}"
            cp "${MODULES_DIR}/${relative}" .
        fi
    done

git_get_exact_tag and new tags

Adding a new git tag does not force a re-configure if cmake variables with git_get_exact_tag

git_get_exact_tag(GIT_TAG)
if(NOT GIT_TAG)
    set(GIT_TAG "")
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/gitinfo.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/gitinfo.cpp)

Some files:

gitinfo.h

extern const char g_GIT_TAG[];

gitinfo.cpp.in

#define GIT_TAG "@GIT_TAG@"
const char g_GIT_TAG[] = GIT_TAG;

CreateLauncher: needs to be ported to generator expressions - else violates policy (use of the LOCATION) -> CMP0026

After recently upgrading to cmake 2.8.12 I get lots of warnings like this:

1> CMake Warning (dev) at App/cmake/CreateLaunchers.cmake:204 (get_target_property):
1> Policy CMP0026 is not set: Disallow use of the LOCATION target property.
1> Run "cmake --help-policy CMP0026" for policy details. Use the cmake_policy
1> command to set the policy and suppress this warning.
1>
1> The LOCATION property should not be read from target "CppExtTest". Use the
1> target name directly with add_custom_command, or use the generator
1> expression $<TARGET_FILE>, as appropriate.
1>
1> Call Stack (most recent call first):
1> App/cmake/CreateLaunchers.cmake:281 (_launcher_produce_vcproj_user)
1> App/CMakeLists.txt:264 (create_target_launcher)
1> App/CppExt/UnitTests/CMakeLists.txt:32 (PL_ADD_UNIT_TEST)
1> This warning is for project developers. Use -Wno-dev to suppress it.

And here is the long description:

cmake --help-policy CMP0026

CMP0026

Disallow use of the LOCATION target property.

CMake 2.8.12 and lower allowed reading the LOCATION target property to
determine the eventual location of build targets. This relies on the
assumption that all necessary information is available at
configure-time to determine the final location and filename of the
target. However, this property is not fully determined until later at
generate-time. At generate time, the $<TARGET_FILE> generator
expression can be used to determine the eventual LOCATION of a target
output.

Code which reads the LOCATION target property can be ported to use the
$<TARGET_FILE> generator expression together with the file(GENERATE)
subcommand to generate a file containing the target location.

The OLD behavior for this policy is to allow reading the LOCATION
property from build-targets. The NEW behavior for this policy is to
not to allow reading the LOCATION property from build-targets.

This policy was introduced in CMake version 3.0.0. CMake version
2.8.12.20140106-gff6f80 warns when the policy is not set and uses OLD behavior.
Use
the cmake_policy command to set it to OLD or NEW explicitly.

FindCppcheck fails with cppcheck 1.5.3

Hi,

I'm using your cmake-modules to apply cppcheck to our sources (thanks!), however I found a small issue when using cppcheck version 1.5.3.

In cppcheck version 1.5.3, the unused functions test is enabled with: --enable=unusedFunction, however in your Findcppcheck.cmake module you have this as:
--enable=unusedFunctions
(note the additional 's').

I'm not sure what the earlier behaviour was but the current version does not work.
Since this is a trivial fix I haven't created a pull request, but I'll try putting a diff in an additional comment.

Thanks,
Brett

GetGitRevisionDescription: git_local_changes() returns false positive when repo. source file touched

My project contains a source file detailing software version information, which I touch at the start of every build to ensure that it is unconditionally built such that a project build timestamp derived from C __DATE__ and __TIME__ macros is up to date.

The git diff-index --quiet HEAD -- command used in the git_local_changes() function results in the repo. being evaluated as dirty once a source file has been touched, and doesn't come right until some other git operation performs an index refresh.

See https://stackoverflow.com/questions/34807971 for a discussion of git diff-index behaviour.

Seems to me that using git diff --quiet HEAD -- instead results in desired behaviour. Alternately can issue git update-index --refresh before the git diff-index call.

git revision description only trigger rebuild of one file

I've been using your git revision description module. Works great!

If I understand this module correctly, it watches files in .git/ and then copies them to CMakeFiles/ which then triggers a rebuild.

Since it triggers a complete rebuild, I simply include the description variable as a preprocessor macro.

I'm wondering if there is a way to trigger a rebuild of only one file. Perhaps have a git_revision.cpp.in that looks like

#include "git_revision.hpp"

const static std::string git_revision = "@git_description@";

and git_revision.hpp

#ifndef GIT_REVISION_HPP
#define GIT_REVISION_HPP

const static std::string git_revision;

#endif

and only trigger a rebuild of git_revision.cpp when a committ, pull, etc. occurs.

Any ideas?

find_package(Markdown) finding some file on Windows but not able to execute it

cc @russell-taylor who knows more about the circumstances since it's his machine.

Bug is presumably in https://github.com/rpavlik/cmake-modules/blob/master/FindMarkdown.cmake - perhaps there are specific executables to exclude, or a test we can do to determine if a given executable actually is a markdown parser at config time before we try to use it.

Here was the error message he got:

2>  Converting Markdown CONTRIBUTING.md to HTML in C:/tmp/vs2013_64/OSVR-Core/CONTRIBUTING.html...
2>  Access is denied.
2>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.CppCommon.targets(170,5): error MSB6006: "cmd.exe" exited with code 5.

Russ, what's the value of MARKDOWN_EXECUTABLE in that config?

VS Launcher with different RUNTIME_LIBRARY_DIR's per configuration

First, thanks a lot for sharing your cmake modules. Especially CreateLaunchers.cmake is really helpful.
But here comes the problem: Some of the dll's have the same name for debug and release builds, but are in different directories. So it would be really cool to be able to write this:

create_target_launcher(myprog
ARGS "/config Default_3D"
RUNTIME_LIBRARIES_Debug ${PL_RUNTIME_DIRS_DEBUG}
RUNTIME_LIBRARIES_Release ${PL_RUNTIME_DIRS_RELEASE}
RUNTIME_LIBRARIES_RelWithDebInfo ${PL_RUNTIME_DIRS_RELEASE}
RUNTIME_LIBRARIES_MinSizeRel ${PL_RUNTIME_DIRS_RELEASE}
WORKING_DIRECTORY ${PROJECT_MAIN_DIR}/App/bin
)

I'm not sure if it's easier to try to emulate it with the ENVIRONMENT option and the VS variable $(ConfigurationName), or if it's easier to modify the cmake code.
What do you think?

Support to find SDL2 installed by vcpkg on Windows

Current FindSDL2.cmake only supports to find SDL2 in MingW environment on Window. We can download and install sdl2 with command ./vcpkg install sdl2:x64-windows on Windows, and it's great if FindSDL2.cmake can support this situation.

Added the MSVC11 support to CreateLaunchers.cmake

I added to my own version support for MSVC11 compiler in CreateLaunchers.cmake:

    if(MSVC11)
        set(LAUNCHER_LINESEP "\n")
        set(USERFILE_VC_VERSION 11.00)
        set(USERFILE_EXTENSION user)
        set(VCPROJ_TYPE vcxproj)
    elseif(MSVC10)

FindOpenSceneGraph.cmake fails to get the version from OpenSceneGraph 3.6

I'm trying to build using OpenSceneGraph branch 3.6 that I built from their git repository, and the CMake module fails in the following way:

[ FindOpenSceneGraph.cmake:157 ] Failed to parse version number, please report this as a bug
-- Found osgDB: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgDB.so  
-- Found osgViewer: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgViewer.so  
-- Found osgText: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgText.so  
-- Found osgGA: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgGA.so  
-- Found osgParticle: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgParticle.so  
-- Found osgUtil: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgUtil.so  
-- Found osgFX: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgFX.so  
-- Found osgShadow: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgShadow.so  
-- Found osgAnimation: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosgAnimation.so  
-- Found osg: /home/dmbaturin/opt/openmw/osg-openmw/lib/libosg.so  
-- Found OpenThreads: /home/dmbaturin/opt/openmw/osg-openmw/lib/libOpenThreads.so  
CMake Error at /usr/share/cmake/Modules/FindOpenSceneGraph.cmake:226 (message):
  ERROR: Version 3.3.4 or higher of the OSG is required.  Version ..  was
  found.
Call Stack (most recent call first):
  CMakeLists.txt:358 (find_package)

I'm ready to do any additional testing if required.

The include/osg/Version file looks fine.

#ifndef OSG_VERSION
#define OSG_VERSION 1

#include <osg/Export>

extern "C" {

#define OPENSCENEGRAPH_MAJOR_VERSION    3
#define OPENSCENEGRAPH_MINOR_VERSION    6
#define OPENSCENEGRAPH_PATCH_VERSION    5
#define OPENSCENEGRAPH_SOVERSION        162

Document how to use GetGitRevisionDescription "offline"

I am not a CMake expert; I'm far more accustomed to raw GNU Make. One of the idioms I usually bake into build systems is automatic version updating such that the version string is determined from git tags and updated on each commit. This works great for simplifying release workflows, and as long as everybody (including automated deployments, etc.) builds from git working trees it works great.

For more traditional software release paradigms where a source tarball is distributed, this is a little more complicated. Since people will not always be building from a git working tree but may be working from a source tarball, one has to include a fallback to determine the version number.

Using GNU Make based builds I know how to do this by bundling a version file with the latest string into the source, and falling back to reading it in the event git is not available. This way make dist or whatever will produce a tarball with an internal data file that can be read in lieu of git describe.

I am trying to contribute a similar workflow to a project that uses CMake. I've successfully included GetGitRevisionDescription from this repository and adapted the build to fetch the version string and define project() using it.

include(GetGitRevisionDescription)
git_describe(GIT_VERSION --tags --always)
string(REGEX REPLACE "-([0-9]*).*" ".\\1" VERSION "${GIT_VERSION}")
project (myproject
	VERSION "${VERSION}")

What I can't figure out is where and how I should cache this project data such that the project can also be build from source tarballs.

It would be nice to see the documentation comments for GetGitRevisionDescription include some notes on the best way to go about using it "offline".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.