Skip to content

Commit

Permalink
Adding an option to build a DLL under windows
Browse files Browse the repository at this point in the history
This adds a BUILD_LIBRARY flag, allowing developers to create libwebvtt.dll. Added instructions for doign this in a MinGW environment.
  • Loading branch information
steve-downing committed Jun 12, 2020
1 parent b63a76a commit 4edb930
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
SET(GCC_COVERAGE_COMPILE_FLAGS "-Wno-narrowing")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

add_definitions(-DWEBVTT_BUILD_LIBRARY)
if (BUILD_LIBRARY)
set(-DWEBVTT_BUILD_LIBRARY 1)
endif (BUILD_LIBRARY)

add_subdirectory("src")
add_subdirectory("test")
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,31 @@ Once built, the static library and include files are available at these location
* Static library: `build/src/webvttxx/libwebvttxx.a`
* Include: `include/webvttxx`

### Building A dll on Windows (MinGW)
[CMake](https://cmake.org/) is also used on Windows for running the builds and tests, but it's recommended to use [MSYS2](https://www.msys2.org/) to create the Mingw64 build environment.

When using CMake, it's recommended that builds take place outside of the main project source, such as in a `build` directory.

1. [Download, install, and update MSYS2 using the steps on the MSYS2 homepage](https://www.msys2.org/)
2. Open the MSYS2 terminal, choosing "Mingw-w64 64 bit" when prompted
3. Change directories to the working folder where you want to build libwebvtt. Keep in mind that the C:\ for Windows is mounted under `/c/` in MinGW. For example, if the build location is "C:\Users\username\sources\", then do `cd /c/users/username/sources/`

```bash
# clone the project
git clone https://github.com/alexa/webvtt.git

# change directories to the project root
cd webvtt

# create and enter the build directory
mkdir build && cd build

# configure the project with CMake, using the MSYS generator
cmake -G 'MSYS Makefiles' -DBUILD_LIBRARY=1 ..

# compile the DLL
make libwebvtt
```

## Running Tests:

Expand Down
4 changes: 4 additions & 0 deletions include/webvtt/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ extern "C" {
# define WEBVTT_CALLBACK __cdecl
# if WEBVTT_BUILD_LIBRARY
# define WEBVTT_EXPORT __declspec(dllexport)
/* Expose private functions necessary to run the unit tests. */
# define WEBVTT_INTERN __declspec(dllexport)
# elif !WEBVTT_STATIC
# define WEBVTT_EXPORT __declspec(dllimport)
# else
Expand All @@ -64,6 +66,8 @@ extern "C" {
# if WEBVTT_OS_WIN32
# if WEBVTT_BUILD_LIBRARY
# define WEBVTT_EXPORT __declspec(dllexport)
/* Expose private functions necessary to run the unit tests. */
# define WEBVTT_INTERN __declspec(dllexport)
# elif !WEBVTT_STATIC
# define WEBVTT_EXPORT __declspec(dllimport)
# else
Expand Down
31 changes: 22 additions & 9 deletions src/webvtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)

add_definitions("-DACSDK_LOG_MODULE=captionslib")
add_library(libwebvtt STATIC
alloc.c
cue.c
cuetext.c
error.c
lexer.c
node.c
parser.c
string.c)

if (BUILD_LIBRARY AND WIN32 OR WIN64)
add_library(libwebvtt SHARED
alloc.c
cue.c
cuetext.c
error.c
lexer.c
node.c
parser.c
string.c)
else (BUILD_LIBRARY AND WIN32 OR WIN64)
add_library(libwebvtt STATIC
alloc.c
cue.c
cuetext.c
error.c
lexer.c
node.c
parser.c
string.c)
endif (BUILD_LIBRARY AND WIN32 OR WIN64)

target_include_directories(libwebvtt PUBLIC
"${libwebvtt_SOURCE_DIR}"
Expand Down
13 changes: 9 additions & 4 deletions src/webvttxx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)


add_library(libwebvttxx STATIC
abstract_parser.cpp
file_parser.cpp)
if (BUILD_LIBRARY AND WIN32 OR WIN64)
add_library(libwebvttxx OBJECT
abstract_parser.cpp
file_parser.cpp)
else (BUILD_LIBRARY AND WIN32 OR WIN64)
add_library(libwebvttxx STATIC
abstract_parser.cpp
file_parser.cpp)
endif (BUILD_LIBRARY AND WIN32 OR WIN64)

target_include_directories(libwebvttxx PUBLIC
"${PROJECT_SOURCE_DIR}/include")
Expand Down
5 changes: 5 additions & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ target_link_libraries(unittests
set(TEST_FILE_DIR "${PROJECT_SOURCE_DIR}/test/unit")
configure_file(test_config.h.in test_config.h @ONLY)

if (WIN32 OR WIN64)
set(CMAKE_CXX_STANDARD_LIBRARIES "-static-libgcc -static-libstdc++ ${CMAKE_CXX_STANDARD_LIBRARIES}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,--no-whole-archive")
endif (WIN32 OR WIN64)

add_test(NAME libwebvtt_tests COMMAND unittests)

0 comments on commit 4edb930

Please sign in to comment.