Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ parser.tab.*

# Build results

src/catch/

[Dd]ebug/
[Rr]elease/
x64/
Expand Down
135 changes: 112 additions & 23 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
cmake_minimum_required(VERSION 3.4)
cmake_minimum_required(VERSION 3.4...3.28)
cmake_policy(SET CMP0054 NEW)
cmake_policy(SET CMP0048 NEW)

project(cppparser)
project(cppparser VERSION 0.1.0)

enable_testing()

include("../common/cmake/conf.cmake")
include("conf.cmake")

set(CMAKE_CXX_STANDARD 17)

Expand All @@ -17,9 +19,18 @@ add_subdirectory(third_party/btyacc_tp)

add_definitions(-DBOOST_AUTO_LINK_NOMANGLE)

add_subdirectory(../common/third_party/boost_tp/cmake ${CMAKE_BINARY_DIR}/boost)
#add_subdirectory(../common/third_party/boost_tp/cmake ${CMAKE_BINARY_DIR}/boost)

include_directories(../common/third_party)
#include_directories(../common/third_party)

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost COMPONENTS filesystem program_options date_time REQUIRED)

if (NOT EXISTS "${CMAKE_SOURCE_DIR}/src/catch/catch.hpp")
file(DOWNLOAD "https://github.com/catchorg/Catch2/releases/download/v2.13.10/catch.hpp" "${CMAKE_SOURCE_DIR}/src/catch/catch.hpp")
endif()

#############################################
## CppParser
Expand All @@ -37,9 +48,9 @@ else()
message(STATUS "Found 'flex' as ${FLEX}")
endif()

set_source_files_properties(src/parser.tab.cpp GENERATED)
set_source_files_properties(src/parser.lex.cpp GENERATED)
set_source_files_properties(src/parser.tab.h GENERATED)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}src/parser.tab.cpp GENERATED)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}src/parser.lex.cpp GENERATED)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}src/parser.tab.h GENERATED)

add_custom_command(
OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/parser.tab.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/parser.tab.h
Expand Down Expand Up @@ -72,18 +83,20 @@ set(CPPPARSER_SOURCES
)

add_library(cppparser STATIC ${CPPPARSER_SOURCES})
add_dependencies(cppparser btyacc boost_filesystem boost_program_options)
#add_dependencies(cppparser btyacc boost_filesystem boost_program_options)
target_link_libraries(cppparser
PUBLIC
boost_filesystem
boost_program_options
boost_system
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_System}
)
target_include_directories(
cppparser
PUBLIC
pub
../common/third_party/boost_tp
${Boost_INCLUDE_DIRS}
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/pub>
$<INSTALL_INTERFACE:${CPPPARSER_INSTALL_INCLUDE_DIR}>
#../common/third_party/boost_tp
PRIVATE
hack
)
Expand All @@ -93,6 +106,82 @@ target_compile_definitions(
YY_NO_UNPUT
)

#install(TARGETS cppparser EXPORT cppparser-config)
#export(TARGETS cppparser NAMESPACE cppparser:: FILE "${CMAKE_CURRENT_BINARY_DIR}/cppparser-config.cmake")
#install(DIRECTORY pub/ DESTINATION include/cppparser)
#install(EXPORT cppparser-config DESTINATION cmake)

# Install rule for headers
install(
DIRECTORY pub/
DESTINATION ${CPPPARSER_INSTALL_INCLUDE_DIR}
COMPONENT Development
)

install(
TARGETS cppparser
EXPORT ${export_config_name}Targets
ARCHIVE DESTINATION ${CPPPARSER_INSTALL_LIBRARY_DIR} COMPONENT Development
LIBRARY DESTINATION ${CPPPARSER_INSTALL_LIBRARY_DIR} COMPONENT RuntimeLibraries
RUNTIME DESTINATION ${CPPPARSER_INSTALL_BIN_DIR} COMPONENT RuntimeLibraries
)

#------------------------------------------------------------------------------
# Configure <export_config_name>ConfigVersion.cmake common to build and install tree
include(CMakePackageConfigHelpers)
set(config_version_file ${PROJECT_BINARY_DIR}/${export_config_name}ConfigVersion.cmake)
write_basic_package_version_file(
${config_version_file}
VERSION "${CPPPARSER_VERSION}"
COMPATIBILITY ExactVersion
)

#------------------------------------------------------------------------------
# Export '<export_config_name>Targets.cmake' for a build tree
export(
EXPORT ${PROJECT_NAME}Targets
FILE "${CMAKE_CURRENT_BINARY_DIR}/${export_config_name}Targets.cmake"
)

# Configure '<export_config_name>Config.cmake' for a build tree
set(build_config ${CMAKE_BINARY_DIR}/${export_config_name}Config.cmake)
configure_package_config_file(
${export_config_name}Config.cmake.in
${build_config}
INSTALL_DESTINATION "${PROJECT_BINARY_DIR}"
)

#------------------------------------------------------------------------------
# Export '<export_config_name>Targets.cmake' for an install tree
install(
EXPORT ${export_config_name}Targets
FILE ${export_config_name}Targets.cmake
DESTINATION ${CPPPARSER_INSTALL_CONFIG_DIR}
)

set(install_config ${PROJECT_BINARY_DIR}/CMakeFiles/${export_config_name}Config.cmake)
configure_package_config_file(
${export_config_name}Config.cmake.in
${install_config}
INSTALL_DESTINATION ${CPPPARSER_INSTALL_CONFIG_DIR}
)

# Install config files
install(
FILES ${config_version_file} ${install_config}
DESTINATION "${CPPPARSER_INSTALL_CONFIG_DIR}"
)


#include(CMakePackageConfigHelpers)
#write_basic_package_version_file(
# "cppparserConfigVersion.cmake"
# VERSION ${cppparser_VERSION}
# COMPATIBILITY AnyNewerVersion)

#install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cppparser-config.cmake" DESTINATION lib/cmake/cppparser)
# "${CMAKE_CURRENT_BINARY_DIR}/cppparserConfigVersion.cmake"

#############################################
## CppParserTest

Expand All @@ -103,9 +192,9 @@ add_executable(cppparsertest
target_link_libraries(cppparsertest
PRIVATE
cppparser
boost_filesystem
boost_program_options
boost_system
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_System}
)

set(E2E_TEST_DIR ${CMAKE_CURRENT_LIST_DIR}/test/e2e)
Expand Down Expand Up @@ -145,9 +234,9 @@ target_include_directories(cppparserunittest
target_link_libraries(cppparserunittest
PRIVATE
cppparser
boost_filesystem
boost_program_options
boost_system
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_System}
)
set(UNIT_TEST_DIR ${CMAKE_CURRENT_LIST_DIR}/test/unit)
add_test(
Expand Down Expand Up @@ -175,9 +264,9 @@ target_compile_definitions(cppparserembeddedsnippetvalidity
target_link_libraries(cppparserembeddedsnippetvalidity
PRIVATE
cppparser
boost_filesystem
boost_program_options
boost_system
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${Boost_System}
)

if(NOT MSVC)
Expand Down
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,47 @@ TEST_CASE("Parsing hello world program")
### Get the source

```sh
git clone https://github.com/satya-das/common.git
git clone https://github.com/satya-das/CppParser.git
git clone https://github.com/agladilin/cpppqqarser.git
```

## Configure and build

### Windows Visual Studio

[Install vcpkg](https://vcpkg.io/en/getting-started) to fetch boost

```powershell
vcpkg install boost-filesystem boost-program-options boost-system boost-date-time
cd cppparser
mkdir build
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE="[path to vcpkg]/scripts/buildsystems/vcpkg.cmake"
#cd build
#msbuild cppparser.sln
cmake --build build --config Debug
.\build\Debug\cppparsertest.exe
.\build\Debug\cppparserunittest.exe
cmake --build build --config Debug --target install # requires administrator privileges
vcpkg install cppparser --overlay-ports=c:\dev\custom-overlay
```

To remove

```powershell
vcpkg remove cppparser
del $VCPKG_ROOT\buildtrees\cppparser
del $VCPKG_ROOT\packages\cppparser_x64-windows
del $VCPKG_ROOT\downloads\agladilin-cppparser-0.1.0.tar.gz
del C:\Users\<user>\AppData\Local\vcpkg\archives\19 # or whatever corresponding to package ABI hash in install log
```

### Linux and others

*For Ubuntu to fetch boost:*

```sh
sudo apt install libboost-dev libboost-system-dev libboost-filesystem-dev libboost-program-options-dev
```

```sh
cd cppparser
mkdir builds
Expand Down
45 changes: 45 additions & 0 deletions conf.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@


if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++1z -fvisibility=hidden -fvisibility-inlines-hidden")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fvisibility=hidden")
else()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wall -std=c++1z -fvisibility=hidden -fvisibility-inlines-hidden")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fvisibility=hidden")
endif()

set(CMAKE_CXX_STANDARD 17)

# Define DLLEXPORT and DLLIMPORT
if(WIN32 OR CYGWIN )
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(DLLEXPORT "__attribute__((dllexport))")
set(DLLIMPORT "")
else()
set(DLLEXPORT "__declspec(dllexport)")
set(DLLIMPORT "__declspec(dllimport)")
endif()
else()
set(DLLEXPORT "__attribute__((visibility (\"default\")))")
set(DLLIMPORT "")
endif()

set(export_config_name ${PROJECT_NAME})

if(NOT DEFINED CPPPARSER_INSTALL_INCLUDE_DIR)
set(CPPPARSER_INSTALL_INCLUDE_DIR include/cppparser)
endif()

if(NOT DEFINED CPPPARSER_INSTALL_BIN_DIR)
set(CPPPARSER_INSTALL_BIN_DIR bin)
endif()

if(NOT DEFINED CPPPARSER_INSTALL_LIBRARY_DIR)
set(CPPPARSER_INSTALL_LIBRARY_DIR lib)
endif()

if(NOT DEFINED CPPPARSER_INSTALL_CONFIG_DIR)
set(CPPPARSER_INSTALL_CONFIG_DIR ${CPPPARSER_INSTALL_LIBRARY_DIR}/cmake/${export_config_name})
endif()
7 changes: 7 additions & 0 deletions cppparserConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@PACKAGE_INIT@

set(export_config_name "@export_config_name@")

set_and_check(${export_config_name}_TARGETS "${CMAKE_CURRENT_LIST_DIR}/${export_config_name}Targets.cmake")

include(${${export_config_name}_TARGETS})
3 changes: 3 additions & 0 deletions pub/cppwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class CppWriter
virtual void emitUsingDecl(const CppUsingDecl* usingDecl,
std::ostream& stm,
CppIndent indentation = CppIndent()) const;
virtual void emitUsingNamespaceDecl(const CppUsingNamespaceDecl* usingDecl,
std::ostream& stm,
CppIndent indentation = CppIndent()) const;
virtual void emitTypedefList(const CppTypedefList* typedefList,
std::ostream& stm,
CppIndent indentation = CppIndent()) const;
Expand Down
20 changes: 20 additions & 0 deletions src/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ git clone https://github.com/satya-das/CppParser.git

## Configure and build

### Windows Visual Studio

[Install vcpkg](https://vcpkg.io/en/getting-started) to fetch boost

```powershell
vcpkg install boost-filesystem boost-program-options boost-system
cd cppparser
mkdir build
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE="[path to vcpkg]/scripts/buildsystems/vcpkg.cmake"
cd build
msbuild cppparser.sln
```

### Linux and others

* For Ubuntu to fetch boost:
```sh
sudo apt install libboost-system-dev libboost-filesystem-dev libboost-program-options-dev
```

```sh
cd cppparser
mkdir builds
Expand Down
9 changes: 9 additions & 0 deletions src/cppwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ void CppWriter::emit(const CppObj* cppObj, std::ostream& stm, CppIndent indentat
return emitDocComment((CppDocComment*) cppObj, stm, indentation);
case CppObjType::kUsingDecl:
return emitUsingDecl((CppUsingDecl*) cppObj, stm, indentation);
case CppObjType::kUsingNamespaceDecl:
return emitUsingNamespaceDecl((CppUsingNamespaceDecl*) cppObj, stm, indentation);
case CppObjType::kTypedefName:
return emitTypedef((CppTypedefName*) cppObj, stm, indentation);
case CppObjType::kTypedefNameList:
Expand Down Expand Up @@ -411,6 +413,13 @@ void CppWriter::emitUsingDecl(const CppUsingDecl* usingDecl,
stm << ";\n";
}

void CppWriter::emitUsingNamespaceDecl(const CppUsingNamespaceDecl* usingDecl,
std::ostream& stm,
CppIndent indentation /* = CppIndent()*/) const
{
stm << indentation << "using namespace " << usingDecl->name_ << ";\n";
}

void CppWriter::emitTypedefList(const CppTypedefList* typedefList,
std::ostream& stm,
CppIndent indentation /* = CppIndent()*/) const
Expand Down
2 changes: 1 addition & 1 deletion src/parser.l
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ For this very reason this file does not use any class that are defined in cppast

%{
// C++17 causes problem when register is used and flex uses register in generated code
#define register
//#define register

#include "cpptoken.h"
#include "cppvarinit.h"
Expand Down
2 changes: 1 addition & 1 deletion src/parser.tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
#define DTORDECL 387
#define YYERRCODE 256

#line 166 "/home/dassat/github/cppparser/src/parser.y"
#line 166 "/home/user/dev/cppparser/src/parser.y"
typedef union {
CppToken str;
CppNtFuncDeclData funcDeclData;
Expand Down
1 change: 1 addition & 0 deletions test/app/compare.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once

#include <iostream>
#include <fstream>
#include <sstream>

#include <boost/filesystem.hpp>
Expand Down
Loading