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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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.


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.

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.

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

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.


# -----------------
# EXAMPLE
# -----------------
if(LINENOISE_BUILD_EXAMPLE)
add_executable(${PROJECT_NAME}-example example.c)
target_link_libraries(${PROJECT_NAME}-example
PRIVATE ${PROJECT_NAME}
)
target_compile_options(${PROJECT_NAME}
PRIVATE -Wall -W
)
endif()

# -----------------
# 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?

install (TARGETS ${PROJECT_NAME}-example
DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif()

install (TARGETS ${PROJECT_NAME}
EXPORT ${PROJECT_NAME}Config
DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
Comment on lines +41 to +44
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.


install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/linenoise.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/linenoise/
)

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.

DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)