Oragon Efreet
Oragon Efreet

Reputation: 1134

CMake : Flex custom_command never called

I have some issue using CMake with Flex in a C++ project. I use FindFlexand its macro FLEX_TARGET which generates a custom command which simply called the Flex executable to create a source file from a .l file.

See macro definition from Github Cmake project.

My project only consists in the CMakeLists.txt and my Example.l LEX file. Also a pair of files, Parser.hpp/Parser.cpp, which currently only contains an "I do nothing" class (constructor, destructor, that's all):

Here's the CMakeLists.txt: cmake_minimum_required(VERSION 3.0)

project(PARSER)

find_package(FLEX REQUIRED)
flex_target(Scanner Example.l ${CMAKE_CURRENT_BINARY_DIR}/LexParser.cpp)

set(Parse_SRCS
    Parse.cpp
    ${FLEX_Scanner_OUTPUTS} 
)

set(Parse_INCS
    Parse.hpp
)

add_executable(Parse ${Parse_INCS} ${Parse_SRCS})

target_include_directories(Parse PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(Parse PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")

Problem : Scanner target is never called.

First checks :

Thank you !

Upvotes: 1

Views: 478

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54589

The function flex_target, somewhat misleadingly, creates a custom command and not a custom target.

Custom commands (unlike custom targets) are not executed by themselves, but have to be associated with a target (or another custom command) that consumes its output.

You do this by adding the output of flex_target, which is written by the function to the FLEX_Scanner_OUTPUTS variable, as a source file to your Parse executable. This will cause the file to be generated when that executable is built.

Note however that custom commands are always executed at build time (eg. when running make), not when running CMake.

Upvotes: 4

Related Questions