From 1678e6fca1b74e180458deaaab41e742a8a5a544 Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Wed, 25 Jul 2018 14:05:50 -0700 Subject: [PATCH 1/6] Start building as a static library on Windows with python2 and winflexbison in the program path --- CMakeLists.txt | 57 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4c8b3e..e61ef77 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,13 +5,17 @@ SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" "${CMAKE_MODULE_PATH}") INCLUDE(version) -SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") - FIND_PACKAGE(PythonInterp 2 REQUIRED) IF (NOT PYTHON_VERSION_MAJOR EQUAL 2) MESSAGE(FATAL_ERROR "Python 2 is required.") ENDIF() +IF(UNIX) + SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h") +ELSEIF(WIN32) + SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h --wincompat") +ENDIF() + FIND_PROGRAM(CTYPESGEN_FOUND ctypesgen.py) FIND_PACKAGE(BISON 3) @@ -21,7 +25,7 @@ IF (BISON_FOUND) ENDIF() IF(FLEX_FOUND) - FLEX_TARGET(GraphQLScanner lexer.lpp ${CMAKE_CURRENT_SOURCE_DIR}/lexer.cpp COMPILE_FLAGS "--header-file=lexer.h") + FLEX_TARGET(GraphQLScanner lexer.lpp ${CMAKE_CURRENT_SOURCE_DIR}/lexer.cpp COMPILE_FLAGS ${FLEX_COMPILE_FLAGS}) IF (BISON_FOUND) ADD_FLEX_BISON_DEPENDENCY(GraphQLScanner graphqlparser) ENDIF() @@ -31,7 +35,7 @@ FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/c) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) -ADD_LIBRARY(graphqlparser SHARED +SET(GRAPHQLPARSER_SOURCES JsonVisitor.cpp ${CMAKE_CURRENT_BINARY_DIR}/Ast.h ${CMAKE_CURRENT_BINARY_DIR}/Ast.cpp @@ -52,10 +56,19 @@ ADD_LIBRARY(graphqlparser SHARED lexer.h GraphQLParser.cpp) -# Enable this and remove CMAKE_CXX_FLAGS fiddle above when we are able -# to upgrade to CMake 2.8.12. Blocker seems to be Travis CI being on -# Ubuntu Precise; Trusty has 2.8.12. -# TARGET_COMPILE_OPTIONS(graphqlparser PUBLIC -std=gnu++11) +IF(UNIX) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") + + ADD_LIBRARY(graphqlparser SHARED ${GRAPHQLPARSER_SOURCES}) + + # Enable this and remove CMAKE_CXX_FLAGS fiddle above when we are able + # to upgrade to CMake 2.8.12. Blocker seems to be Travis CI being on + # Ubuntu Precise; Trusty has 2.8.12. + # TARGET_COMPILE_OPTIONS(graphqlparser PUBLIC -std=gnu++11) +ELSEIF(WIN32) + ADD_LIBRARY(graphqlparser STATIC ${GRAPHQLPARSER_SOURCES}) +ENDIF() + ADD_EXECUTABLE(dump_json_ast dump_json_ast.cpp) TARGET_LINK_LIBRARIES(dump_json_ast graphqlparser) @@ -104,10 +117,11 @@ INSTALL(FILES stack.hh syntaxdefs.h DESTINATION include/graphqlparser) -INSTALL(TARGETS graphqlparser - LIBRARY DESTINATION lib) if (UNIX) + INSTALL(TARGETS graphqlparser + LIBRARY DESTINATION lib) + # generate pkgconfig file include(FindPkgConfig QUIET) if(PKG_CONFIG_FOUND) @@ -116,16 +130,19 @@ if (UNIX) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libgraphqlparser.pc" DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig") endif() -endif() -IF (test) - ADD_SUBDIRECTORY(test) + IF (test) + ADD_SUBDIRECTORY(test) - if(UNIX) - # setup valgrind - ADD_CUSTOM_TARGET(memcheck - valgrind --leak-check=full --suppressions=./test/valgrind.supp --dsymutil=yes --error-exitcode=1 ./test/runTests >/dev/null - ) - endif() + if(UNIX) + # setup valgrind + ADD_CUSTOM_TARGET(memcheck + valgrind --leak-check=full --suppressions=./test/valgrind.supp --dsymutil=yes --error-exitcode=1 ./test/runTests >/dev/null + ) + endif() -ENDIF() + ENDIF() +elseif(WIN32) + INSTALL(TARGETS graphqlparser + ARCHIVE DESTINATION lib) +endif() From 69a926201434d8e02b906635d57cf3e8cd96250e Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Wed, 25 Jul 2018 17:31:20 -0700 Subject: [PATCH 2/6] Switch to a SHARED library on Windows --- CMakeLists.txt | 47 +++++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e61ef77..3c61120 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,24 @@ PROJECT(libgraphqlparser C CXX) SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" "${CMAKE_MODULE_PATH}") +IF(UNIX) + SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h") +ELSEIF(WIN32) + # If we're building this with vcpkg on Windows, let portfile.cmake tell us where it + # stored these tools. Otherwise these variables should be empty and we'll fall back + # to the normal CMake FIND_PACKAGE logic for each of these programs. + SET(CMAKE_PROGRAM_PATH + "${VCPKG_DOWNLOADS_PYTHON2_DIR}" + "${VCPKG_DOWNLOADS_FLEX_DIR}" + "${VCPKG_DOWNLOADS_BISON_DIR}" + "${CMAKE_PROGRAM_PATH}") + + SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h --wincompat") + + # Let CMake figure out the exports for the SHARED library (DLL) on Windows. + SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) +ENDIF() + INCLUDE(version) FIND_PACKAGE(PythonInterp 2 REQUIRED) @@ -35,7 +53,7 @@ FILE(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/c) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}) INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}) -SET(GRAPHQLPARSER_SOURCES +ADD_LIBRARY(graphqlparser SHARED JsonVisitor.cpp ${CMAKE_CURRENT_BINARY_DIR}/Ast.h ${CMAKE_CURRENT_BINARY_DIR}/Ast.cpp @@ -56,19 +74,10 @@ SET(GRAPHQLPARSER_SOURCES lexer.h GraphQLParser.cpp) -IF(UNIX) - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") - - ADD_LIBRARY(graphqlparser SHARED ${GRAPHQLPARSER_SOURCES}) - - # Enable this and remove CMAKE_CXX_FLAGS fiddle above when we are able - # to upgrade to CMake 2.8.12. Blocker seems to be Travis CI being on - # Ubuntu Precise; Trusty has 2.8.12. - # TARGET_COMPILE_OPTIONS(graphqlparser PUBLIC -std=gnu++11) -ELSEIF(WIN32) - ADD_LIBRARY(graphqlparser STATIC ${GRAPHQLPARSER_SOURCES}) -ENDIF() - +# Enable this and remove CMAKE_CXX_FLAGS fiddle above when we are able +# to upgrade to CMake 2.8.12. Blocker seems to be Travis CI being on +# Ubuntu Precise; Trusty has 2.8.12. +# TARGET_COMPILE_OPTIONS(graphqlparser PUBLIC -std=gnu++11) ADD_EXECUTABLE(dump_json_ast dump_json_ast.cpp) TARGET_LINK_LIBRARIES(dump_json_ast graphqlparser) @@ -118,10 +127,11 @@ INSTALL(FILES syntaxdefs.h DESTINATION include/graphqlparser) -if (UNIX) - INSTALL(TARGETS graphqlparser - LIBRARY DESTINATION lib) +INSTALL(TARGETS graphqlparser + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +if (UNIX) # generate pkgconfig file include(FindPkgConfig QUIET) if(PKG_CONFIG_FOUND) @@ -142,7 +152,4 @@ if (UNIX) endif() ENDIF() -elseif(WIN32) - INSTALL(TARGETS graphqlparser - ARCHIVE DESTINATION lib) endif() From 7bcd1f3c374c99c8f0b2422a044b18b9498aca4e Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Wed, 25 Jul 2018 17:51:33 -0700 Subject: [PATCH 3/6] Add the import .lib and the .pdb symbols to the installed files on Windows --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c61120..d499038 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,4 +152,9 @@ if (UNIX) endif() ENDIF() +elseif(WIN32) + INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/graphqlparser.lib + DESTINATION lib) + INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/graphqlparser.pdb + DESTINATION bin) endif() From c06cad636840b871e038b49c1999e78b397dd48f Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Wed, 25 Jul 2018 18:41:22 -0700 Subject: [PATCH 4/6] Put back the UNIX -std=gnu++11 flag --- CMakeLists.txt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d499038..d213704 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,8 @@ PROJECT(libgraphqlparser C CXX) SET(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake" "${CMAKE_MODULE_PATH}") IF(UNIX) - SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h") + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11") + SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h") ELSEIF(WIN32) # If we're building this with vcpkg on Windows, let portfile.cmake tell us where it # stored these tools. Otherwise these variables should be empty and we'll fall back @@ -29,9 +30,9 @@ IF (NOT PYTHON_VERSION_MAJOR EQUAL 2) ENDIF() IF(UNIX) - SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h") + SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h") ELSEIF(WIN32) - SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h --wincompat") + SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h --wincompat") ENDIF() FIND_PROGRAM(CTYPESGEN_FOUND ctypesgen.py) From e86095467cb1bb3ba5bc41823f36e28fdad79203 Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Thu, 6 Sep 2018 16:15:14 -0700 Subject: [PATCH 5/6] Cleanup some duplicate defines and move the test block out of if(UNIX) --- CMakeLists.txt | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d213704..a4c92a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,12 +29,6 @@ IF (NOT PYTHON_VERSION_MAJOR EQUAL 2) MESSAGE(FATAL_ERROR "Python 2 is required.") ENDIF() -IF(UNIX) - SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h") -ELSEIF(WIN32) - SET(FLEX_COMPILE_FLAGS "--header-file=lexer.h --wincompat") -ENDIF() - FIND_PROGRAM(CTYPESGEN_FOUND ctypesgen.py) FIND_PACKAGE(BISON 3) @@ -141,21 +135,20 @@ if (UNIX) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libgraphqlparser.pc" DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/pkgconfig") endif() - - IF (test) - ADD_SUBDIRECTORY(test) - - if(UNIX) - # setup valgrind - ADD_CUSTOM_TARGET(memcheck - valgrind --leak-check=full --suppressions=./test/valgrind.supp --dsymutil=yes --error-exitcode=1 ./test/runTests >/dev/null - ) - endif() - - ENDIF() elseif(WIN32) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/graphqlparser.lib DESTINATION lib) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/graphqlparser.pdb DESTINATION bin) endif() + +IF (test) + ADD_SUBDIRECTORY(test) + + if(UNIX) + # setup valgrind + ADD_CUSTOM_TARGET(memcheck + valgrind --leak-check=full --suppressions=./test/valgrind.supp --dsymutil=yes --error-exitcode=1 ./test/runTests >/dev/null + ) + endif() +ENDIF() From 8e1e0c7d12db23ceb4701f83e40f60c7e682884b Mon Sep 17 00:00:00 2001 From: Bill Avery Date: Thu, 6 Sep 2018 17:31:56 -0700 Subject: [PATCH 6/6] Get the unit tests building and passing on Windows --- test/CMakeLists.txt | 15 +++++++++++---- test/ParserTests.cpp | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7e22436..cdfdb5f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,12 @@ -ADD_SUBDIRECTORY(googletest-release-1.8.0/googletest) +IF(UNIX) + ADD_SUBDIRECTORY(googletest-release-1.8.0/googletest) + INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) +ELSEIF(WIN32) + FIND_PACKAGE(GTest REQUIRED) +ENDIF() ENABLE_TESTING() -INCLUDE_DIRECTORIES(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) - FILE(COPY valgrind.supp DESTINATION .) FILE(COPY kitchen-sink.graphql DESTINATION .) @@ -16,7 +19,11 @@ FILE(COPY schema-kitchen-sink.json DESTINATION .) ADD_EXECUTABLE(runTests ParserTests.cpp JsonVisitorTests.cpp BuildCAPI.c) -TARGET_LINK_LIBRARIES(runTests gtest gtest_main) +IF(UNIX) + TARGET_LINK_LIBRARIES(runTests gtest gtest_main) +ELSEIF(WIN32) + TARGET_LINK_LIBRARIES(runTests GTest::GTest GTest::Main) +ENDIF() TARGET_LINK_LIBRARIES(runTests graphqlparser) diff --git a/test/ParserTests.cpp b/test/ParserTests.cpp index 299f7fb..43fc125 100644 --- a/test/ParserTests.cpp +++ b/test/ParserTests.cpp @@ -315,7 +315,7 @@ TEST(ParserTests, ProducesCorrectOutputForSchemaKitchenSink) { } static void expectSchemaParsing(const char *queryStr) { - char buf[strlen("1.1-XXX: schema support disabled") + 1]; + char buf[sizeof("1.1-XXX: schema support disabled")]; ASSERT_LT(strlen(queryStr), 999); snprintf( buf,