Giter Club home page Giter Club logo

clangformat-cmake's Introduction

language.badge license.badge

ClangFormat.cmake module

ClangFormat.cmake is a simple CMake module for clang-format support.

Requirements

The module requires CMake 3.0 or higher and some version of clang-format installed.

Integration

  1. Obtain the module and add it into your project's CMake modules path:

    • Copy approach:

      $ wget https://raw.githubusercontent.com/zemasoft/clangformat-cmake/master/cmake/ClangFormat.cmake -P cmake
      # CMakeLists.txt
      
      list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
    • Submodule approach:

      $ git submodule add https://github.com/zemasoft/clangformat-cmake
      # CMakeLists.txt
      
      list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/clangformat-cmake/cmake)
  2. Include the module:

    # CMakeLists.txt
    
    include(ClangFormat)
  3. Setup the module:

    # CMakeLists.txt
    
    clangformat_setup(
      src/hello.hpp
      src/hello.cpp
    )

    or:

    # CMakeLists.txt
    
    target_clangformat_setup(sometarget)

Usage

  1. Generate the build system:

    $ cmake -S . -Bbuild
  2. Format sources anytime using clangformat target:

    $ cmake --build build --target clangformat

    or:

    $ cd build
    $ make clangformat

Example

See an example here.

clangformat-cmake's People

Contributors

developerpaul123 avatar zemasoft avatar

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

Watchers

 avatar  avatar

clangformat-cmake's Issues

target_clangformat_setup() usage in project with multiple targets

As far as I can tell it is not possible to use this CMake module in projects with hierarchical CMakeLists.txt and/or multiple CMake targets.

Assuming project with an exectuable and library target it would be nice to be able to use the CMake module as follows.

project(my_app)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/clangformat-cmake/cmake)
include(ClangFormat)

add_library(my_lib my_lib.cpp)
target_clangformat_setup(my_lib)

add_executable(my_app my_app.cpp)
target_clangformat_setup(my_app)
target_link_libraries(my_app my_lib)

However this will currently fail because each call to target_clangformat_setup() results in a call to add_custom_target() with the same target name (${PROJECT_NAME}_clangformat).

A possible workaround would be to use clangformat_setup() directly. However, this would require to list all source files twice and does not work with hierarchical CMakeLists.txt setups: assuming you have a separate CMakeLists.txt for the executable and the library from the above example.

CMakeLists.txt for executable

project(my_app)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/clangformat-cmake/cmake)
include(ClangFormat)

add_subdirectory(lib)

add_executable(my_app my_app.cpp)
clangformat_setup(my_app.cpp)
target_link_libraries(my_app my_lib)

CMakeLists.txt for library

add_library(my_lib my_lib.cpp)
clangformat_setup(my_lib.cpp)

This will also fail because clangformat_setup() is called twice in the same project.

edit: I have just discovered your example repository that demonstrates usage of the CMake module. In the example, the problem is avoided by having separate project() command in the main and test CMakeLists.txt. Is this a CMake pattern when using hierarchical CMakeLists.txt? However, this will still fail if multiple targets are defined in the same CMakeLists.txt file.

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.