Skip to content

Commit

Permalink
Add support for CMake
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Jul 6, 2017
1 parent 9871f1f commit da04c67
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 5 deletions.
34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
cmake_minimum_required(VERSION 3.0)
project(fakevim VERSION 0.0.1)

# Library name
set(bin fakevim)

# Public headers
set(${bin}_public_headers
fakevim/fakevimactions.h
fakevim/fakevimhandler.h
)

# Source files common for all platforms
set(${bin}_sources
fakevim/fakevimactions.cpp
fakevim/fakevimhandler.cpp
fakevim/utils/qtcassert.cpp
${${bin}_public_headers}
)

# Required pkg-config packages
set(${bin}_pkg_config_requires)

include(cmake/cxx11.cmake)
include(cmake/library.cmake)
include(cmake/qt.cmake)
include(cmake/pkg-config.cmake)

# Files with Q_OBJECT macros to pass to moc utility
set(CMAKE_INCLUDE_CURRENT_DIR ON)
qt5_wrap_cpp(${bin}_mocced "fakevim/fakevimhandler.h")
# Generate moc file with correct file name.
qt5_generate_moc("fakevim/fakevimhandler.cpp" "fakevimhandler.moc")
target_sources(${bin} PRIVATE ${${bin}_mocced} "fakevimhandler.moc")
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ To build the library and simple example editor (in `example` directory), run fol
qmake
make

Preferably use **CMake to install** the library.

cmake -DCMAKE_INSTALL_PREFIX=/usr/local .
make install

Build and run **example with **QMake**:

cd example
qmake && make
./example

Build and run **example with **CMake**:

cd example
fakevim_DIR=/usr/local/lib/cmake/fakevim cmake && make
./fakevim_example

Build and run **tests with QMake**:

cd tests
qmake && make
./tests

Build and run **tests with CMake**:

cd tests
fakevim_DIR=/usr/local/lib/cmake/fakevim cmake && make
./fakevim_tests

Supported Features
------------------

Expand Down
5 changes: 5 additions & 0 deletions cmake/cxx11.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.1)
set(CMAKE_CXX_STANDARD 11)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()
1 change: 1 addition & 0 deletions cmake/fakevimConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("${CMAKE_CURRENT_LIST_DIR}/fakevimTargets.cmake")
85 changes: 85 additions & 0 deletions cmake/library.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
set(INSTALL_LIB_DIR "lib" CACHE PATH
"Installation directory for libraries")
set(INSTALL_BIN_DIR "bin" CACHE PATH
"Installation directory for executables")
set(INSTALL_INCLUDE_DIR "include/${bin}" CACHE PATH
"Installation directory for header files")
set(INSTALL_CMAKE_DIR "lib/cmake/${bin}" CACHE PATH
"Installation directory for CMake files")

# Shared or static library
option(CREATE_STATIC_LIBRARY "Create static library" OFF)
if (CREATE_STATIC_LIBRARY)
set(libtype STATIC)
else()
set(libtype SHARED)
endif()

add_library(${bin} ${libtype} ${${bin}_sources})
set_target_properties(${bin} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)

# Headers
set(exports_dir "${CMAKE_CURRENT_BINARY_DIR}/exports")
set(export_header "${exports_dir}/private/${bin}_export.h")
set_target_properties(${bin} PROPERTIES
PUBLIC_HEADER "${${bin}_public_headers}"
PRIVATE_HEADER "${export_header}")

target_include_directories(${bin}
PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/${bin}>
INTERFACE $<INSTALL_INTERFACE:include/${bin}>
)

install(
TARGETS ${bin} EXPORT ${bin}Targets
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT bin
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" COMPONENT lib
PUBLIC_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}" COMPONENT dev
PRIVATE_HEADER DESTINATION "${INSTALL_INCLUDE_DIR}/private" COMPONENT dev
)

# Generate and install CMake files for the library so `find_package(<Library>)` can be used with CMake.
# For more info: https://cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html#creating-packages
include(GenerateExportHeader)
generate_export_header(${bin} EXPORT_FILE_NAME "${export_header}")
target_include_directories(${bin} PRIVATE "${exports_dir}")

include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/${bin}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)

export(
EXPORT ${bin}Targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${bin}Targets.cmake"
)
configure_file(
cmake/${bin}Config.cmake
"${CMAKE_CURRENT_BINARY_DIR}/${bin}Config.cmake"
COPYONLY
)

install(
EXPORT ${bin}Targets
FILE
${bin}Targets.cmake
DESTINATION
"${INSTALL_CMAKE_DIR}"
COMPONENT
dev
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${bin}Config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${bin}ConfigVersion.cmake"
DESTINATION
"${INSTALL_CMAKE_DIR}"
COMPONENT
dev
)
22 changes: 22 additions & 0 deletions cmake/pkg-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set(PKG_CONFIG_REQUIRES "${${bin}_pkg_config_requires}")
set(PKG_CONFIG_LIBDIR "\${prefix}/lib")
set(PKG_CONFIG_INCLUDEDIR "\${prefix}/include/${bin}")
set(PKG_CONFIG_LIBS "-L\${libdir} -l${bin}")
set(PKG_CONFIG_CFLAGS "-I\${includedir}")

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/pkg-config.pc.in"
"${CMAKE_CURRENT_BINARY_DIR}/${bin}.pc"
)

set(INSTALL_PKG_CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig"
CACHE PATH "Installation directory for pkg-config files")

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${bin}.pc"
DESTINATION
"${INSTALL_PKG_CONFIG_DIR}"
COMPONENT
dev
)
6 changes: 6 additions & 0 deletions cmake/qt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
find_package(Qt5Widgets REQUIRED)

include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
target_link_libraries(${bin} Qt5::Widgets)

set(${bin}_pkg_config_requires ${${bin}_pkg_config_requires} Qt5::Widgets)
15 changes: 15 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 2.8.8)
project(fakevim_example)

find_package(fakevim REQUIRED)
find_package(Qt5Widgets REQUIRED)

set(bin fakevim_example)
add_executable(${bin} main.cpp editor.cpp)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
qt5_generate_moc("editor.cpp" "editor.moc")
target_sources(${bin} PRIVATE "editor.moc")

target_link_libraries(${bin} fakevim)
target_link_libraries(${bin} Qt5::Widgets)
6 changes: 4 additions & 2 deletions fakevim/fakevimactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <utils/savedaction.h>
*/

#include "private/fakevim_export.h"

#include <QCoreApplication>
#include <QHash>
#include <QObject>
Expand Down Expand Up @@ -136,8 +138,8 @@ class FakeVimSettings
QHash<int, QString> m_codeToName;
};

FakeVimSettings *theFakeVimSettings();
FakeVimAction *theFakeVimSetting(int code);
FAKEVIM_EXPORT FakeVimSettings *theFakeVimSettings();
FAKEVIM_EXPORT FakeVimAction *theFakeVimSetting(int code);

} // namespace Internal
} // namespace FakeVim
8 changes: 5 additions & 3 deletions fakevim/fakevimhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#pragma once

#include "private/fakevim_export.h"

#include <QObject>
#include <QTextEdit>

Expand All @@ -42,7 +44,7 @@ enum RangeMode
RangeBlockAndTailMode // Ctrl-v for D and X
};

struct Range
struct FAKEVIM_EXPORT Range
{
Range() {}
Range(int b, int e, RangeMode m = RangeCharMode);
Expand All @@ -54,7 +56,7 @@ struct Range
RangeMode rangemode = RangeCharMode;
};

struct ExCommand
struct FAKEVIM_EXPORT ExCommand
{
ExCommand() {}
ExCommand(const QString &cmd, const QString &args = QString(),
Expand All @@ -80,7 +82,7 @@ enum MessageLevel
MessageShowCmd // partial command
};

class FakeVimHandler : public QObject
class FAKEVIM_EXPORT FakeVimHandler : public QObject
{
Q_OBJECT

Expand Down
42 changes: 42 additions & 0 deletions fakevim/private/fakevim_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#ifndef FAKEVIM_EXPORT_H
#define FAKEVIM_EXPORT_H

#ifdef FAKEVIM_STATIC_DEFINE
# define FAKEVIM_EXPORT
# define FAKEVIM_NO_EXPORT
#else
# ifndef FAKEVIM_EXPORT
# ifdef fakevim_EXPORTS
/* We are building this library */
# define FAKEVIM_EXPORT __attribute__((visibility("default")))
# else
/* We are using this library */
# define FAKEVIM_EXPORT __attribute__((visibility("default")))
# endif
# endif

# ifndef FAKEVIM_NO_EXPORT
# define FAKEVIM_NO_EXPORT __attribute__((visibility("hidden")))
# endif
#endif

#ifndef FAKEVIM_DEPRECATED
# define FAKEVIM_DEPRECATED __attribute__ ((__deprecated__))
#endif

#ifndef FAKEVIM_DEPRECATED_EXPORT
# define FAKEVIM_DEPRECATED_EXPORT FAKEVIM_EXPORT FAKEVIM_DEPRECATED
#endif

#ifndef FAKEVIM_DEPRECATED_NO_EXPORT
# define FAKEVIM_DEPRECATED_NO_EXPORT FAKEVIM_NO_EXPORT FAKEVIM_DEPRECATED
#endif

#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef FAKEVIM_NO_DEPRECATED
# define FAKEVIM_NO_DEPRECATED
# endif
#endif

#endif
11 changes: 11 additions & 0 deletions pkg-config.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Name: ${PROJECT_NAME}
Description: ${PROJECT_DESCRIPTION}
Version: ${PROJECT_VERSION}
Requires: ${PKG_CONFIG_REQUIRES}

prefix=${CMAKE_INSTALL_PREFIX}
includedir=${PKG_CONFIG_INCLUDEDIR}
libdir=${PKG_CONFIG_LIBDIR}

Libs: ${PKG_CONFIG_LIBS}
Cflags: ${PKG_CONFIG_CFLAGS}
17 changes: 17 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 2.8.8)
project(fakevim_tests)

find_package(fakevim REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Test REQUIRED)

set(bin fakevim_tests)
add_executable(${bin} fakevim_test.cpp ../example/editor.cpp)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
qt5_wrap_cpp(${bin}_mocced "fakevimplugin.h")
qt5_generate_moc("../example/editor.cpp" "editor.moc")
target_sources(${bin} PRIVATE ${${bin}_mocced} "editor.moc")

target_link_libraries(${bin} fakevim)
target_link_libraries(${bin} Qt5::Widgets Qt5::Test)

0 comments on commit da04c67

Please sign in to comment.