I am writing an opencl kernel with many helper function in the same .cl file. I want to package the helper function into a header file. I read the spec and see I should pass -I Dir in the clCreateProgram function call.
I am still confused here. When I crate a A.h file to be included in the .cl file. Should I create a corresponding A.c file or A.cl file?
If I create a corresponding A.c file, some routines in .c file and .cl file might be different and might make the function cannot be used directly by the kernel.
You can have a header file and #include that in the kernel file. So to include file1.h in kernel.cl you need to add #include "file1.h" and as build option pass -I. if header file is in the same folder as kernel file or i.e. -I./include if in the include folder.
Related
How can I find a file inside a directory based on the file extension?
I want to create a function that finds a linker script. The function will be defined in the toolchain file, because each compiler has a different type of linker script.
I want to pass to the function a path to a directory with linker files for all type of supported compilers.
The function implemented for the specific compiler will find the right linker file (for example, armcc compiler will find the file with the extension .sct)
Thanks for the help.
Background
I have a number of headers as part of a library (call it A) and are also used externally from other libraries (call it C).
I'd like to compile as part A's compilation to ensure the headers are self-contained. (Currently, this involves compiling C and if there are issues, re-compile A & make a new release).
Question
What's the best way to compile headers and discard the results? I am only interested in their successful compilation.
I am thinking of copy and rename them as cpp files (it's a C++ project) and then create a library out of them to check errors. But is there a simpler solution?
My aim is to the command-line equivalent of
g++ [compile_flags] -c header.hpp
and check for errors but not interested in the produced files.
I'd like something that works with cmake 3.13.5 (or older).
Thanks
Since I ran into the same question recently, let me show the solution I went with:
When you list the header files in your target's sources and only use relative paths below the current path (i.e. no .. in the paths), you can use a function like the following to compile a separate cpp file for each hpp file.
function(check_headers target)
# build object library used to "compile" the headers
add_library(${target}_headers OBJECT)
target_link_libraries(${target}_headers PRIVATE ${target})
target_include_directories(${target}_headers PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
# add a proxy source file for each header in the target source list
get_target_property(TARGET_SOURCES ${target} SOURCES)
foreach(TARGET_SOURCE ${TARGET_SOURCES})
if ("${TARGET_SOURCE}" MATCHES ".*\.hpp$")
set(HEADER_SOURCEFILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_SOURCE}.cpp")
file(WRITE "${HEADER_SOURCEFILE}" "#include \"${TARGET_SOURCE}\"")
target_sources(${target}_headers PRIVATE "${HEADER_SOURCEFILE}")
endif()
endforeach()
endfunction()
It adds an object library that compiles each header file in the given target.
So I'm transitioning from make to cmake and I'm stuck at a point which is trivial in make because of in-source-dir building.
The following scenario:
Directory A/ generates some header, A.h.
In order for other cpp/h, non-generated files to find the generated header, I've added the ${CMAKE_CURRENT_BINARY_DIR} as an include (<--assumption 1 here for being best approach)
However now in directory B/ (a sibling directory of A/) there is a particular source file which transitively through a file.h, which is also in A/, needs the generated A.h file.
The problem I face now is that file.h has the reference to A.h as #include "A.h" but at this point in the cmake execution the only include directories are:
B/
Now.. what is the best way to include A.h in directory B, should I hardcode something like: ${CMAKE_BINARY_DIR}/path/to/include/for/A/A.h that feels bad though
Another way is to generate the A.h file to the source dir, however I often see advise to not generate anything to the source directory.
You can use PROJECT_BINARY_DIR variable to set up location of generated file from different directories. Note that PROJECT_BINARY_DIR change every time you use project command. In this case you can use <PROJECT>_BINARY_DIR variable:
# CMakeLists.txt
project(Foo)
add_subdirectory(Boo)
# Boo/CMakeLists.txt
project(Boo)
message("Boo_BINARY_DIR: ${Boo_BINARY_DIR}") # == PROJECT_BINARY_DIR
message("Foo_BINARY_DIR: ${Foo_BINARY_DIR}") # != PROJECT_BINARY_DIR
I need to compile protocol buffer .proto files to .pb.cc,.pb.h files. There is a program for this conversion.
protoc test.proto --cpp_out .
How can I add such a generic rule in cmake? I can do this with add_custom_command. But I have to this for every .proto file. Is there a better way to do this?
It looks like CMake's FindProtobuf module provides this functionality via the function PROTOBUF_GENERATE_CPP.
You can pass multiple .proto files in the one call, e.g.
file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
Note that even though the CMakeLists.txt file which calls find_package(Protobuf) could be the top-level one, the CMakeLists.txt file(s) which invoke the function would need to be in the same directory as the .proto files.
I'm trying to include a precompiled header file into my project by adding -include myprecompiledheader.h to my compiler flags. However, when I use the -H option in GCC, this file doesn't show up in the list and thus I can't look at the "!" or "x" to determine if my .pch file is being used or not. Is there some way to tell if my precompiled header is being used when I -include it?
You can use a dummy header which contains only an #error directive, and put that in the same place as the .gch file. For example:
project/precompiled.h // your precompiled header
project/pre.h.gch // precompiled.h compiled into a gch
project/pre.h // contains only "#error "Not using the GCH."
Then, when you -include pre.h, if the compiler doesn't use the gch file it'll throw an error.