Generate flags for PlanCompiler via cmake

Summary:
Add join function to functions.cmake

This is a convenience function which will join a list of values by
replacing ';' with the given separator.

cmake: Add fetching compile flags in gcc format

This is a utility function which takes all target compilation flags that
can be passed to gcc or clang.

Generate flags for PlanCompiler via cmake

Cmake will now collect all compiler options and definitions which are
then stored in a generated `query/plan_compiler.hpp`. The generated file
is not tracked by git and is stored inside cmake's build directory. The
file is fast to generate and may change often depending on the build
type.

Additionally, link and include directories are also generated as
absolute paths. In the future, we may want to support relative paths so
that copying/installing the build dir creates runnable binaries.

Add -Wall flag for all build types

Lowercase #pragma once in tests for network_common

It seems 'once' may be case sensitive, since clang outputs a warning for
it that the pragma is unknown.

Reviewers: buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D107
This commit is contained in:
Teon Banek 2017-03-09 11:42:16 +01:00
parent 9dada557a3
commit 333861e7e9
4 changed files with 72 additions and 36 deletions

View File

@ -62,7 +62,7 @@ find_package(Threads REQUIRED)
# c++14
# TODO: set here 17 once it will be available in the cmake version (3.8)
set(cxx_standard 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z -Wall")
# -----------------------------------------------------------------------------
# dir variables
@ -357,12 +357,14 @@ set(memgraph_src_files
# STATIC library used by memgraph executables
add_library(memgraph_lib STATIC ${memgraph_src_files})
add_dependencies(memgraph_lib generate_opencypher_parser)
add_dependencies(memgraph_lib generate_opencypher_parser
generate_plan_compiler_flags)
# -----------------------------------------------------------------------------
# STATIC PIC library used by query engine
add_library(memgraph_pic STATIC ${memgraph_src_files})
add_dependencies(memgraph_pic generate_opencypher_parser)
add_dependencies(memgraph_pic generate_opencypher_parser
generate_plan_compiler_flags)
set_property(TARGET memgraph_pic PROPERTY POSITION_INDEPENDENT_CODE TRUE)
# -----------------------------------------------------------------------------
@ -462,3 +464,23 @@ foreach(file_path ${__HARDCODED_SOURCES})
PROPERTIES RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/__hardcoded_targets")
endforeach()
get_target_cxx_flags(memgraph_lib compile_flags)
set(plan_compiler_flags_file ${build_include_dir}/query/plan_compiler_flags.hpp)
add_custom_target(generate_plan_compiler_flags
COMMENT
"Generating ${plan_compiler_flags_file} with compile flags: ${compile_flags}"
VERBATIM
COMMAND
/bin/echo -e
"#pragma once"
"\\n// Generated from cmake. Flags are taken from current build configuration."
"\\n// Do not edit this manually!"
"\\nstatic constexpr const char compile_flags[] = \"${compile_flags}\";"
"\\nstatic constexpr const char include_dirs[] ="
"\"-I${CMAKE_BINARY_DIR}/include -I${CMAKE_SOURCE_DIR}/libs/fmt\";"
"\\nstatic constexpr const char link_dirs[] = \"-L${CMAKE_BINARY_DIR}\";"
> ${plan_compiler_flags_file}
BYPRODUCTS ${plan_compiler_flags_file}
)
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES ${plan_compiler_flags_file})

View File

@ -43,3 +43,45 @@ function(disallow_in_source_build)
"directory, e.g. 'build' and run cmake there.")
endif()
endfunction()
# Takes a string of ';' separated VALUES and stores a new string in RESULT,
# where ';' is replaced with given SEP.
function(join values sep result)
# Match non escaped ';' and replace it with separator. This doesn't handle
# the case when backslash is escaped, e.g: "a\\\\;b" will produce "a;b".
string(REGEX REPLACE "([^\\]|^);" "\\1${sep}" tmp "${values}")
# Fix-up escapes by matching backslashes and removing them.
string(REGEX REPLACE "[\\](.)" "\\1" tmp "${tmp}")
set(${result} "${tmp}" PARENT_SCOPE)
endfunction()
# Returns a list of compile flags ready for gcc or clang.
function(get_target_cxx_flags target result)
# First set the CMAKE_CXX_FLAGS variables, then append directory and target
# options in that order. Definitions come last, directory then target.
string(TOUPPER ${CMAKE_BUILD_TYPE} build_type)
set(flags "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${build_type}}")
get_directory_property(dir_opts DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMPILE_OPTIONS)
if(dir_opts)
join("${dir_opts}" " " dir_opts)
string(APPEND flags " " ${dir_opts})
endif()
get_target_property(opts ${target} COMPILE_OPTIONS)
if(opts)
join("${opts}" " " opts)
string(APPEND flags " " ${opts})
endif()
get_directory_property(dir_defs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMPILE_DEFINITIONS)
if(dir_defs)
join("${dir_defs}" " -D" dir_defs)
string(APPEND flags " -D" ${dir_defs})
endif()
get_target_property(defs ${target} COMPILE_DEFINITIONS)
if(defs)
join("${defs}" " -D" defs)
string(APPEND flags " -D" ${defs})
endif()
set(${result} ${flags} PARENT_SCOPE)
endfunction()

View File

@ -5,6 +5,7 @@
#include "logging/default.hpp"
#include "logging/loggable.hpp"
#include "query/exception/plan_compilation.hpp"
#include "query/plan_compiler_flags.hpp"
#include "utils/string/join.hpp"
// TODO:
@ -27,45 +28,16 @@ class PlanCompiler : public Loggable {
* @return void
*/
void compile(const std::string &in_file, const std::string &out_file) {
std::string flags;
// TODO: sync this with cmake configuration
#ifdef NDEBUG
flags += " -DNDEBUG -O2";
#endif
#ifdef LOG_NO_TRACE
flags += " -DLOG_NO_TRACE";
#endif
#ifdef LOG_NO_DEBUG
flags += " -DLOG_NO_DEBUG";
#endif
#ifdef LOG_NO_INFO
flags += " -DLOG_NO_INFO";
#endif
#ifdef LOG_NO_WARN
flags += " -DLOG_NO_WARN";
#endif
#ifdef LOG_NO_ERROR
flags += " -DLOG_NO_ERROR";
#endif
#ifdef DEBUG_ASSERT_ON
flags += " -DDEBUG_ASSERT_ON";
#endif
// TODO: load from config (generate compile command)
// generate compile command
auto compile_command = utils::prints(
"clang++" + flags,
"clang++", compile_flags,
#ifdef HARDCODED_OUTPUT_STREAM
"-DHARDCODED_OUTPUT_STREAM",
#endif
// "-std=c++1y -O2 -DNDEBUG",
"-std=c++1y", // compile flags
in_file, // input file
"-o", out_file, // ouput file
"-I./include", "-I../include", "-I../../include", "-I../../../include",
"-I./libs/fmt", "-I../libs/fmt", "-I../../libs/fmt",
"-I../../../libs/fmt", "-L./ -L../ -L../../", "-lmemgraph_pic",
include_dirs,
link_dirs, "-lmemgraph_pic",
"-shared -fPIC" // shared library flags
);

View File

@ -1,4 +1,4 @@
#pragma ONCE
#pragma once
#include <array>
#include <cassert>