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

Windows build - WIP #116

Open
wants to merge 5 commits into
base: 2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ tests/bench_str.csv

config.h.in
examples/simple
examples/simple_cpp
examples/simple_cpp

build/
.vscode/
25 changes: 25 additions & 0 deletions BUILD.WINDOWS.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Building with CMAKE on Windows using VCPKG
=======================================

This document describes how to compile, build and install libr3 on Windows using CMake, MSVC compiler and VCPKG

VCPKG can be installed from

https://github.com/Microsoft/vcpkg

Once VCPKG is installed you must install libcheck and pcre before running the folloiwing command to configure build

cmake -H"." -B"<BUILD_DIRECTORY>" -G"<GENERATOR_NAME>" -DCMAKE_TOOLCHAIN_FILE=<VCPKG_TOOLCHAIN_FILE> -DVCPKG_TARGET_TRIPLET=<VCPKG_TARGET_TRIPLET>

Then to perform build

cmake --build -B"<BUILD_DIRECTORY>" --target ALL_BUILD --config "<CMAKE_BUILD_TYPE>"

Then to run tests, you must copy pcre.dll and pcre.pdb file to output directory prior to running tests

cmake --build -B"<BUILD_DIRECTORY>" --target RUN_TESTS --config "<CMAKE_BUILD_TYPE>"

Window build is currently WIP.



28 changes: 16 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,24 @@ find_package(PCRE REQUIRED)
include(CheckSymbolExists)
check_symbol_exists(strdup string.h HAVE_STRDUP)
check_symbol_exists(strndup string.h HAVE_STRNDUP)
check_symbol_exists(asprintf stdio.h HAVE_ASPRINTF)
configure_file(config.h.cmake config.h)

if (NOT WIN32)
# Configure substitutions for r3.pc. The variables set here must match the
# @<values>@ in r3.pc.in.
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix ${prefix})
set(includedir ${prefix}/include)
set(libdir ${prefix}/lib)
set(PACKAGE_VERSION ${PROJECT_VERSION})
configure_file(r3.pc.in r3.pc @ONLY)
install(
FILES
${PROJECT_BINARY_DIR}/r3.pc
DESTINATION lib/pkgconfig)
endif()

add_subdirectory(src)

install(
Expand All @@ -26,18 +42,6 @@ install(
include/r3.hpp
DESTINATION include)

# Configure substitutions for r3.pc. The variables set here must match the
# @<values>@ in r3.pc.in.
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix ${prefix})
set(includedir ${prefix}/include)
set(libdir ${prefix}/lib)
set(PACKAGE_VERSION ${PROJECT_VERSION})
configure_file(r3.pc.in r3.pc @ONLY)
install(
FILES
${PROJECT_BINARY_DIR}/r3.pc
DESTINATION lib/pkgconfig)

if(CHECK_FOUND)
enable_testing()
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ R3

[![Build Status](https://travis-ci.org/c9s/r3.svg?branch=2.0)](https://travis-ci.org/c9s/r3)

[![Windows Build Status](https://img.shields.io/appveyor/ci/simon-p-r/r3/master.svg?label=windows&style=flat-square&maxAge=2592000)](https://ci.appveyor.com/project/simon-p-r/r3)

[![Coverage Status](https://coveralls.io/repos/c9s/r3/badge.svg)](https://coveralls.io/r/c9s/r3)

R3 is an URL router library with high performance, thus, it's implemented in C.
Expand Down
76 changes: 76 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This is the configuration file for AppVeyor builds.
# Look at the following for reference:
# https://www.appveyor.com/docs/appveyor-yml

environment:
vsversion: none
arch: default
matrix:
- platform: vs
vsversion: 2015
arch: x86
- platform: vs
vsversion: 2017
arch: x86
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
- platform: vs
vsversion: 2015
arch: x64
- platform: vs
vsversion: 2017
arch: x64
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017


cache: c:\tools\vcpkg\installed\

# build Configuration, i.e. Debug, Release, etc.
configuration: Release


#---------------------------------#
# scripts that are called at #
# the very beginning, before #
# repo cloning #
#---------------------------------#

init:
- git config --global core.autocrlf input


#---------------------------------#
# scripts to run before build #
#---------------------------------#

before_build:
- cd c:\tools\vcpkg\
- git pull
- bootstrap-vcpkg.bat
- vcpkg integrate install
- vcpkg install check:x86-windows check:x64-windows pcre:x86-windows pcre:x64-windows
- set VCPKG_HOME=c:/tools/vcpkg/

#---------------------------------#
# build code and unit tests #
#---------------------------------#

build_script:
- cd c:\projects\r3
- win-build %configuration% %arch%


#---------------------------------#
# run unit test for all x86 #
# and x64 architecture builds #
#---------------------------------#

# test_script:
# - echo Project directory before running test step...


#---------------------------------#
# build and test completed #
#---------------------------------#

# on_finish:
# - echo Project directory after running tests...
14 changes: 8 additions & 6 deletions cmake/Modules/FindCheck.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,26 @@ ENDIF ( CHECK_MIN_VERSION )
# Look for CHECK include dir and libraries
IF( NOT CHECK_FOUND AND NOT PKG_CONFIG_FOUND )

SET ( CHECK_LIBRARIES "" )
FIND_PATH( CHECK_INCLUDE_DIRS check.h )

FIND_LIBRARY( CHECK_LIBRARIES NAMES check )

IF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
FIND_LIBRARY( CHECK_LIBRARY NAMES check )
IF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARY )
FIND_LIBRARY(CHECK_COMPAT_LIBRARY NAMES compat )
list( APPEND CHECK_LIBRARIES "${CHECK_LIBRARY}" "${CHECK_COMPAT_LIBRARY}" )
SET( CHECK_FOUND 1 )
MESSAGE ( STATUS "Found CHECK: ${CHECK_LIBRARIES}" )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE ( STATUS "Found CHECK: ${CHECK_LIBRARIES}" )
ENDIF ( NOT Check_FIND_QUIETLY )
ELSE ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
ELSE ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARY )
IF ( Check_FIND_REQUIRED )
MESSAGE( FATAL_ERROR "Could NOT find CHECK" )
ELSE ( Check_FIND_REQUIRED )
IF ( NOT Check_FIND_QUIETLY )
MESSAGE( STATUS "Could NOT find CHECK" )
ENDIF ( NOT Check_FIND_QUIETLY )
ENDIF ( Check_FIND_REQUIRED )
ENDIF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARIES )
ENDIF ( CHECK_INCLUDE_DIRS AND CHECK_LIBRARY )
ENDIF( NOT CHECK_FOUND AND NOT PKG_CONFIG_FOUND )

# Hide advanced variables from CMake GUIs
Expand Down
2 changes: 2 additions & 0 deletions config.h.cmake
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#cmakedefine HAVE_STRDUP @HAVE_STRDUP@
#cmakedefine HAVE_STRNDUP @HAVE_STRNDUP@
#cmakedefine HAVE_ASPRINTF @HAVE_ASPRINTF@

4 changes: 4 additions & 0 deletions include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef _WIN32
#include <alloca.h>
#endif


#ifdef __cplusplus
extern "C" {
Expand Down
5 changes: 5 additions & 0 deletions include/r3.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ typedef unsigned char bool;
extern "C" {
#endif

#ifdef _WIN32
#define __attribute__(A)
#endif


struct _edge;
struct _node;
struct _route;
Expand Down
38 changes: 31 additions & 7 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,43 @@ add_library(r3 STATIC
str.c
token.c)

target_compile_definitions(r3
PRIVATE
_GNU_SOURCE)

target_include_directories(r3
PUBLIC
set(R3_INCLUDE_DIRS "")

if(WIN32)
target_compile_definitions(r3
PRIVATE
_CRT_SECURE_NO_WARNINGS
)
list(APPEND
R3_INCLUDE_DIRS
${PROJECT_BINARY_DIR}
${PCRE_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/include
)
else()
target_compile_definitions(r3
PRIVATE
_GNU_SOURCE
)
list(APPEND
R3_INCLUDE_DIRS
${PROJECT_BINARY_DIR}
${PCRE_INCLUDE_DIR}
${PROJECT_SOURCE_DIR}/3rdparty
${PROJECT_SOURCE_DIR}/include)
${PROJECT_SOURCE_DIR}/include
)
endif()

target_include_directories(r3
PUBLIC
${R3_INCLUDE_DIRS}
)

target_link_libraries(r3
PUBLIC
${PCRE_LIBRARIES})
${PCRE_LIBRARIES}
)

install(
TARGETS r3
Expand Down
20 changes: 19 additions & 1 deletion src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#ifndef _WIN32
#include <sys/mman.h>
#include <unistd.h>
#endif
#include "memory.h"


#ifdef _WIN32
#define R3_MEMORY_THREAD __declspec(thread)
#else
#define R3_MEMORY_THREAD __thread
#endif

struct st_r3_mem_recycle_chunk_t {
struct st_r3_mem_recycle_chunk_t *next;
};
Expand All @@ -56,7 +66,7 @@ struct st_r3_mem_pool_shared_ref_t {

void *(*r3_mem__set_secure)(void *, int, size_t) = memset;

static __thread r3_mem_recycle_t mempool_allocator = {16};
static R3_MEMORY_THREAD r3_mem_recycle_t mempool_allocator = {16};

void r3_fatal(const char *msg)
{
Expand Down Expand Up @@ -178,25 +188,31 @@ void r3_mem_link_shared(r3_mem_pool_t *pool, void *p)
link_shared(pool, R3_STRUCT_FROM_MEMBER(struct st_r3_mem_pool_shared_entry_t, bytes, p));
}

#ifndef WIN32
static unsigned int topagesize(unsigned int capacity)
{
unsigned int pagesize = getpagesize();
return (offsetof(r3_buffer_t, _buf) + capacity + pagesize - 1) / pagesize * pagesize;
}
#endif

void r3_buffer__do_free(r3_buffer_t *buffer)
{
/* caller should assert that the buffer is not part of the prototype */
if (buffer->capacity == buffer->_prototype->_initial_buf.capacity) {
r3_mem_free_recycle(&buffer->_prototype->allocator, buffer);
#ifndef _WIN32
} else if (buffer->_fd != -1) {
close(buffer->_fd);
munmap((void *)buffer, topagesize(buffer->capacity));
#endif
} else {
free(buffer);
}
}

#ifndef _WIN32

r3_iovec_t r3_buffer_reserve(r3_buffer_t **_inbuf, unsigned int min_guarantee)
{
r3_buffer_t *inbuf = *_inbuf;
Expand Down Expand Up @@ -294,6 +310,8 @@ r3_iovec_t r3_buffer_reserve(r3_buffer_t **_inbuf, unsigned int min_guarantee)
return ret;
}

#endif

void r3_buffer_consume(r3_buffer_t **_inbuf, unsigned int delta)
{
r3_buffer_t *inbuf = *_inbuf;
Expand Down
6 changes: 4 additions & 2 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// PCRE
#include <pcre.h>

#define MAX_SUBSTRINGS 30

#include "r3.h"
#include "r3_slug.h"
#include "slug.h"
Expand Down Expand Up @@ -329,7 +331,7 @@ R3Node * r3_tree_matchl(const R3Node * n, const char * path, unsigned int path_l
info("COMPARE PCRE_PATTERN\n");
const char *substring_start = 0;
int substring_length = 0;
int ov[ n->ov_cnt ];
int ov[MAX_SUBSTRINGS];
int rc;

info("pcre matching %s on %s\n", n->combined_pattern, path);
Expand Down Expand Up @@ -425,7 +427,7 @@ R3Node * r3_tree_matchl(const R3Node * n, const char * path, unsigned int path_l
if ((e = r3_node_find_edge_str(n, path, path_len))) {
restlen = path_len - e->pattern.len;
if (!restlen) {
return e->child && e->child->endpoint ? e->child : NULL;
return e->child && e->child->endpoint ? e->child : NULL;
}
return r3_tree_matchl(e->child, path + e->pattern.len, restlen, entry);
}
Expand Down
4 changes: 4 additions & 0 deletions src/slug.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "slug.h"
#include "r3_debug.h"

#ifndef HAVE_ASPRINTF
#include "str.h"
#endif



r3_slug_t * r3_slug_new(const char * path, int path_len) {
Expand Down
Loading