Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added CMakeLists.txt to support building via cmake #223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

thorsten-klein
Copy link

Simplify cross compilation and integration into other build systems by using cmake.
Additionally simplify usage within other cmake projects by providing a cmake EXPORT (find_package(linenoise))

@thorsten-klein thorsten-klein changed the title added CMakeLists.txt to support cmake added CMakeLists.txt to build via cmake May 9, 2024
@thorsten-klein thorsten-klein changed the title added CMakeLists.txt to build via cmake added CMakeLists.txt to support building via cmake May 9, 2024
@antirez
Copy link
Owner

antirez commented May 9, 2024

Thank you for your contribution, @thorsten-klein
I believe that small libraries like that should not be required to provide things to work more easily with one or another build system. The Makefile included is just to build the examples. For the same reason, I believe that cmake users can write their own files. I'll keep this issue open so that the file is easy to find :) Thanks.

@thorsten-klein
Copy link
Author

Thank you for your fast reply! 👍🏻

I believe that cmake users can write their own files

What do you mean with this?
Currently there is no usage via cmake possible, except copy&paste the source code into your own project (in order to build the linenoise library).

I think it would be a very big benefit for users with only this small change. Also many other projects (no matter if big or very small) are moving to cmake.

Feel free to merge this PR whenever you want to use cmake as well.
Until then we will keep this CMakeLists.txt file as patch (within our linenoise conan package).

@antirez
Copy link
Owner

antirez commented May 9, 2024

Unfortunately I don't see (for personal tastes) mass-moving to CMake a good thing. So what I meant is that people willing to use CMake, should be able to integrate Linenoise with CMake easily. I find it strange that a build system requires libraries to explicitly adapt to it.

@thorsten-klein
Copy link
Author

thorsten-klein commented May 9, 2024

Out of curiosity:
Why does the Makefile only build the example executable and not a library?
How do expect users to integrate linenoise into their project? Shall they copy the .c file and compile it as part of their project?

I would expect that there is at least some library of linenoise which I can link.
In this case: How would you expect users to integrate a library into their build system?
Isn't it common that tools provide some pkg-config or cmake integration?

@remexre
Copy link

remexre commented May 9, 2024

We ship this pkg-config file in the nixpkgs (NixOS) version, fwiw: https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/libraries/linenoise/linenoise.pc.in

On the other hand, I'd expect most distros wouldn't need to ship anything: you'd put linenoise.h in /usr/include, linenoise.{a,so} in /usr/lib, and most build systems should do the right thing.

If something like this were to be upstreamed, I'd strongly support pkg-config over CMake -- a plain Makefile-based project can much more easily depend on a library that uses pkg-config than one that uses CMake.

@antirez
Copy link
Owner

antirez commented May 9, 2024

Out of curiosity: Why does the Makefile only build the example executable and not a library? How do expect users to integrate linenoise into their project? Shall they copy the .c file and compile it as part of their project?

Because the Makefile is just an example of integration with a toy program. You build it with make and can play with the library in 2 seconds. The library is not built because linenoise "default" usage is to include a copy of it inside your project. It's a single C file, no dependencies, and so forth. Of course who have other needs can take the C file and package it in other ways. But in my opinion this is outside the goals of the project itself.


include(GNUInstallDirs)

option(LINENOISE_BUILD_EXAMPLE OFF)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second argument is for a documentation string.

# -----------------
# LIBRARY
# -----------------
add_library(${PROJECT_NAME} linenoise.c)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not abuse PROJECT_NAME like this.

# -----------------
add_library(${PROJECT_NAME} linenoise.c)
target_include_directories(${PROJECT_NAME}
INTERFACE $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/linenoise>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use install(TARGETS ... INCLUDES DESTINATION) instead for specifying includes for the install.

Comment on lines +15 to +17
target_compile_options(${PROJECT_NAME}
PRIVATE -Wall -W
)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't hardcode toolchain flags. These can be provided from the command line or (more conveniently for developers) a preset.

Comment on lines +41 to +44
install (TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Config
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
Copy link

@friendlyanon friendlyanon May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You set the minimum to be 3.4, but this command invocation relies on defaults provided only by 3.14.
Spell out the RUNTIME, LIBRARY and ARCHIVE destinations or raise the minimum requirement.

cmake_minimum_required(VERSION 3.4)
project(linenoise C)

include(GNUInstallDirs)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please prefer to put things closer to where they are used.

# -----------------
# INSTALLATION
# -----------------
if(TARGET ${PROJECT_NAME}-example)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When and why does an example need to be installed?

@friendlyanon
Copy link

friendlyanon commented May 10, 2024

I believe that small libraries like that should not be required to provide things to work more easily with one or another build system.

The size of a project is absolutely irrelevant when it comes to CMake. The point of providing CMake build tooling that also installs a CMake package that can be found by find_package is to make building and package management easier for downstream users. Every package manager knows how to deal with a well behaved CMake project and how to tweak details that CMake lets us tweak for different scenarios.

I believe that cmake users can write their own files.

CMake makes that possible certainly, but it's also better to have the instructions be written once instead of N times. You also have to figure out how to acquire the project now.
Vendor it? This is routinely removed by package managers, because they usually want to have projects use packages as they already provide them.
Use find_path and find_library manually? You can't be sure as a project maintainer that your audience will be able to make this work.

If the project already provides a CMake package, there is no doubt how to package and consume the project. find_package(linenoise REQUIRED) followed by target_link_libraries(my_target PRIVATE linenoise::linenoise) is a lot simpler than cascading if()/elseif() statements (potentially for every project trying to use linenoise) trying to guess where to get linenoise from.

)

install(EXPORT ${PROJECT_NAME}Config
NAMESPACE ${PROJECT_NAME}::

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add an ALIAS target that matches this name.

@antirez
Copy link
Owner

antirez commented May 10, 2024

The size of a project is absolutely irrelevant when it comes to CMake.

Understand your point of view. I don't agree. This library wants to have zero involvement with building systems and the software "supply chain". So I'll refrain from commenting more :) Peace & Love.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants