Skip to content

Commit

Permalink
Add support for LIBARGS_SHARED with MSVC
Browse files Browse the repository at this point in the history
  • Loading branch information
mzdun committed Oct 18, 2020
1 parent 7d74bee commit 8110a87
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 116 deletions.
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ install:
- sudo update-alternatives --install /usr/bin/gcov gcov /usr/bin/gcov-10 90
# install cmake
- CMAKE_VERSION="3.18"
- CMAKE_FULL="${CMAKE_VERSION}.2"
- CMAKE_FULL="${CMAKE_VERSION}.4"
- pushd ~
- curl "https://cmake.org/files/v${CMAKE_VERSION}/cmake-${CMAKE_FULL}-Linux-x86_64.tar.gz" -o "cmake-${CMAKE_FULL}-Linux-x86_64.tar.gz"
- tar xf "cmake-${CMAKE_FULL}-Linux-x86_64.tar.gz"
- export PATH="${PWD}/cmake-${CMAKE_FULL}-Linux-x86_64/bin:${PATH}"
- popd
- pyenv global 3.8
- python -m pip install -U conan
- export PATH=$PATH:`python -c "import site; import os; print(':'.join([os.path.abspath(os.path.join(dir, '..', '..', '..', 'bin')) for dir in site.getsitepackages()]))"`
- conan user
- conan profile new default --detect
- conan profile update settings.compiler.libcxx=libstdc++11 default

before_script:
# verify installed versions
Expand All @@ -39,6 +45,7 @@ before_script:
script:
- mkdir build
- cd build
- conan install .. --build missing -s build_type=Debug
- cmake -DCMAKE_BUILD_TYPE=Debug -DLIBARGS_COVERALLS=ON -DLIBARGS_COVERALLS_UPLOAD=ON ..
- make -j`nproc`
- make coveralls
20 changes: 16 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
cmake_minimum_required (VERSION 3.12)
project (args
VERSION 0.12.0
cmake_minimum_required(VERSION 3.12)
project(args
VERSION 0.12.1
DESCRIPTION ""
LANGUAGES CXX)

set(PROJECT_VERSION_STABILITY "") # or "-alpha", or "-beta", or "-rc.5"

if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
message(STATUS "Libargs: Standalone")

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)

set(LIBARG_TESTING_DEFAULT ON)
set(LIBARG_INSTALL_DEFAULT ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
Expand All @@ -25,6 +29,7 @@ endif()

set(LIBARGS_TESTING ${LIBARG_TESTING_DEFAULT} CACHE BOOL "Compile and/or run self-tests")
set(LIBARGS_INSTALL ${LIBARG_INSTALL_DEFAULT} CACHE BOOL "Install the library")
set(LIBARGS_SHARED OFF CACHE BOOL "Build an .so instead of the .a archive")

if (LIBARGS_TESTING)
find_package(Python3 COMPONENTS Interpreter REQUIRED)
Expand Down Expand Up @@ -95,25 +100,32 @@ endif()

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/src/version.in" "${CMAKE_CURRENT_BINARY_DIR}/include/args/version.hpp" @ONLY)

add_library(args STATIC
set(BUILD_SHARED_LIBS ${LIBARGS_SHARED})
add_library(args
src/actions.cpp
src/parser.cpp
src/printer.cpp
src/translator.cpp
include/args/actions.hpp
include/args/api.hpp
include/args/parser.hpp
include/args/printer.hpp
include/args/translator.hpp
"${CMAKE_CURRENT_BINARY_DIR}/include/args/version.hpp"
)
target_compile_options(args PRIVATE ${ADDITIONAL_WALL_FLAGS})
target_compile_definitions(args PRIVATE LIBARGS_EXPORTING)
target_compile_features(args PRIVATE cxx_std_17)
target_include_directories(args
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_BINARY_DIR}/include
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)

if (LIBARGS_SHARED)
target_compile_definitions(args PUBLIC LIBARGS_SHARED)
endif()

include(CheckCXXSourceCompiles)
function(check_charconv)
check_cxx_source_compiles("#include <charconv>
Expand Down
76 changes: 71 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,96 @@ The argument values can be stored in:
- enums, with little help from library user (uses `std::is_enum` and needs `args::enum_traits` provided), as well as
- `std::optional`, `std::vector` and `std::unordered_set` of the things on this list.

## Config

Type of all CMake variables blow is `BOOL`.

|Project variable|Comment|
|----------------|-------|
|`LIBARGS_TESTING`|Compile and/or run self-tests. _Adds `test` target (`RUN_TESTS` on MSVC), which runs automatic tests._ |
|`LIBARGS_INSTALL`|Install the library. _Adds `install` target (`INSTALL` on MSVC), which installs all distributable files inside `CMAKE_INSTALL_PREFIX` directory. On MSVC it operates on `Release` configuration._ |
|`LIBARGS_SHARED`|Build an .so instead of the .a archive. _Setting this variable is still experimental. Since this is pre-1.0 code, installing a shared library system-wide is not advised in the first place, in addition there is little experience in using this library as a shared entity._ |
|`LIBARGS_COVERALLS`|Turn on coveralls support. _Ads `coveralls` target, which runs tests and gathers coverage results in the Coveralls.io compatible JSON file. This variable is only available, if `LIBARGS_TESTING` is true. On Ubuntu, it works only with `gcov`, `llvm`-based solution is not available. On MSVC it operates on `Debug` configuration and requires [OpenCppCoverage](https://github.com/OpenCppCoverage/OpenCppCoverage) installed and on `%PATH%`._|

## Building

Conan is used for proper `MD`/`MDd`/`MT`/`MTd` setup. This project does not require any dependencies otherwise. The `<Config>` used in recipes below is either `Debug` or `Release`.

### Ubuntu

```sh
mkdir build && cd build
cmake .. -G Ninja -DLIBARGS_TESTING=OFF -DLIBARGS_INSTALL=OFF
conan install .. -s build_type=<Config>
cmake .. \
-G Ninja \
-DLIBARGS_TESTING=OFF \
-DLIBARGS_INSTALL=OFF \
-DCMAKE_BUILD_TYPE=<Config>
ninja
```

### Windows

The `<RT>` below is either `MD` or `MT` for `Release`, or `MDd` or `MTd` for `Debug`. If `LIBARGS_SHARED` is set to `ON`, then `MD`/`MDd` is strongly advised.

```sh
mkdir build && cd build
conan install .. -s build_type=<Config> -s compiler.runtime=<RT>
cmake .. -G Ninja -DLIBARGS_TESTING=OFF -DLIBARGS_INSTALL=OFF
cmake --build . --target ALL_BUILD -- ^
/nologo /v:m /p:Configuration=<Config>
```

## Testing

### Ubuntu

```sh
mkdir build && cd build
cmake .. -G Ninja -DLIBARGS_TESTING=ON
conan install .. -s build_type=<Config>
cmake .. \
-G Ninja \
-DLIBARGS_TESTING=ON \
-DLIBARGS_INSTALL=OFF \
-DCMAKE_BUILD_TYPE=<Config>
ninja && ninja test
```

### Windows

```sh
mkdir build && cd build
conan install .. -s build_type=<Config> -s compiler.runtime=<RT>
cmake .. -G Ninja -DLIBARGS_TESTING=ON -DLIBARGS_INSTALL=OFF
cmake --build . --target RUN_TESTS -- ^
/nologo /v:m /p:Configuration=<Config>
```

## Installing

### Ubuntu

```sh
mkdir build && cd build
cmake .. -G Ninja -DLIBARGS_TESTING=OFF -DLIBARGS_INSTALL=ON
conan install .. -s build_type=Release
cmake .. \
-G Ninja \
-DLIBARGS_TESTING=OFF \
-DLIBARGS_INSTALL=ON \
-DCMAKE_BUILD_TYPE=Release
ninja && sudo ninja install
```

### Windows

```sh
mkdir build && cd build
conan install .. -s build_type=Release -s compiler.runtime=MD
cmake .. -G Ninja -DLIBARGS_TESTING=OFF -DLIBARGS_INSTALL=ON
cmake --build . --target RUN_TESTS -- ^
/nologo /v:m /p:Configuration=Release
```

## Example

To mimic Python example:
Expand Down Expand Up @@ -115,7 +181,7 @@ prog: error: argument N is required
The arguments with values can have those values either separated by a space or can have it glued to the name of the argument; in case of long names, glued values need to be separated from names by a `"="`.
|Width|Example|Seperated|Glued|Standalone|
|Width|Example|Separated|Glued|Standalone|
|-----|-------|--------------|-----|-------------|
|Long|`"arg"`|`--arg value`|`--arg=value`|`--arg`|
|Short|`"c"`, `"z"`, `"f"`|`-f value`|`-fvalue`|`-c -z`|
Expand Down Expand Up @@ -190,7 +256,7 @@ Extension point for the enum arguments, helping convert strings from command lin
- `ENUM_TRAITS_NAME(enum-value)` or
- `ENUM_TRAITS_NAME_EX(enum-value, "string-to-use")`

which provide the string-to-enum mapping; one-argument version uses `enum-name` as scope for `enum-value`, while two-argument version is much lower level and the value is left unscoped.
which provide the string-to-enum mapping; one-argument version uses `enum-name` as scope for `enum-value`, while two-argument version is much lower level and the value is left not scoped.
3. `ENUM_TRAITS_END(enum-name)` \
finishes all the work started by `ENUM_TRAITS_BEGIN(enum-name)`.

Expand Down
4 changes: 4 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[requires]

[generators]
cmake
28 changes: 17 additions & 11 deletions include/args/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@
#endif
#endif

#include <args/api.hpp>

namespace args {
class parser;
struct base_translator;

namespace actions {
[[noreturn]] void argument_is_not_integer(parser& p,
std::string const& name);
[[noreturn]] void argument_out_of_range(parser& p,
std::string const& name);
[[noreturn]] void enum_argument_out_of_range(parser& p,
std::string const& name,
std::string const& value,
std::string const& values);
[[noreturn]] LIBARGS_API void argument_is_not_integer(
parser& p,
std::string const& name);
[[noreturn]] LIBARGS_API void argument_out_of_range(
parser& p,
std::string const& name);
[[noreturn]] LIBARGS_API void enum_argument_out_of_range(
parser& p,
std::string const& name,
std::string const& value,
std::string const& values);
} // namespace actions

template <typename Storage, typename = void>
Expand Down Expand Up @@ -202,7 +207,7 @@ namespace args {
};

namespace actions {
struct action {
struct LIBARGS_API action {
virtual ~action();
virtual bool required() const = 0;
virtual void required(bool value) = 0;
Expand Down Expand Up @@ -291,7 +296,7 @@ namespace args {

void visited(bool val) { visited_ = val; }

std::string argname(parser&) const;
LIBARGS_API std::string argname(parser&) const;

public:
void required(bool value) override { required_ = value; }
Expand All @@ -305,7 +310,8 @@ namespace args {
}
bool visited() const override { return visited_; }
void meta(std::string_view s) override { meta_ = s; }
std::string meta(base_translator const& _) const override;
LIBARGS_API std::string meta(
base_translator const& _) const override;
void help(std::string_view s) override { help_ = s; }
std::string const& help() const override { return help_; }

Expand Down
22 changes: 22 additions & 0 deletions include/args/api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2018 midnightBITS
// This code is licensed under MIT license (see LICENSE for details)

#pragma once

#if defined(_WIN32)
#define LIBARGS_EXPORT __declspec(dllexport)
#define LIBARGS_IMPORT __declspec(dllimport)
#else
#define LIBARGS_EXPORT
#define LIBARGS_IMPORT
#endif

#if defined(LIBARGS_SHARED)
#if defined(LIBARGS_EXPORTING)
#define LIBARGS_API LIBARGS_EXPORT
#else
#define LIBARGS_API LIBARGS_IMPORT
#endif
#else
#define LIBARGS_API
#endif
Loading

0 comments on commit 8110a87

Please sign in to comment.