Git Product home page Git Product logo

Comments (7)

lexxmark avatar lexxmark commented on July 18, 2024

as I know FlexLexer.h is a mandatory file when you generate C++ lexer. It should be in the same folder where win_flex.exe resides.

It seems Cmake know it somehow and tries to locate it.

from winflexbison.

anphetamina avatar anphetamina commented on July 18, 2024

Is this line necessary in the CMakeLists?
target_link_libraries(${project_name} ${FLEX_LIBRARIES})

from winflexbison.

anphetamina avatar anphetamina commented on July 18, 2024

Adding the line I have mentioned above I always get this CMake error

CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: FL_LIBRARY (ADVANCED)

from winflexbison.

lexxmark avatar lexxmark commented on July 18, 2024

I'm not experienced in CMake and thus cannot help here.

from winflexbison.

LonghronShen avatar LonghronShen commented on July 18, 2024

@anphetamina I think this PR #79 and the mentioned example might help

from winflexbison.

GitMensch avatar GitMensch commented on July 18, 2024

@LonghronShen So we do have a new version with the PR included (available binaries found in the README as CI artifacts) now. Can you please elaborate where the issue with the initial approach is?

from winflexbison.

LonghronShen avatar LonghronShen commented on July 18, 2024

Thank you for the quick merge!

The CMake code snip from the OP:

find_package(BISON)
find_package(FLEX)

The FindFlex and FindBison module bundled with the CMake itself assumes that the Flex and Bison installation is on the default search path. This is not a problem for working on non-Windows platforms, or a custom CMake toolchain is provided. But for Windows users, this is not very easy for CMake to automatically find WinFlexBison without proper installation or settings.

If you don't want to install the WinFlexBison by tools like chocolatey, or write a custom CMake toolchain that defines the search path for flex and bison, with the PR #79 and the CMake modules provided in the example, you can now directly import and add WinFlexBison as a pure CMake dependency like this:

include(FetchContent)

if(WIN32)
    # winflexbision
    FetchContent_Declare(winflexbision 
        GIT_REPOSITORY https://github.com/lexxmark/winflexbison.git
        GIT_TAG master)

    FetchContent_GetProperties(winflexbision)
    if(NOT winflexbision_POPULATED)
        FetchContent_Populate(winflexbision)
        add_subdirectory(${winflexbision_SOURCE_DIR} ${winflexbision_BINARY_DIR} EXCLUDE_FROM_ALL)

        execute_process(COMMAND ${CMAKE_COMMAND}
            -S ${winflexbision_SOURCE_DIR}
            -B ${CMAKE_BINARY_DIR}/external/winflexbision
            -G ${CMAKE_GENERATOR}
            -D CMAKE_BUILD_TYPE=Debug
            -D CMAKE_RUNTIME_OUTPUT_DIRECTORY=${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
            -D CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG=${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}
            -D CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE=${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}
        )

        execute_process(COMMAND ${CMAKE_COMMAND}
            --build ${CMAKE_BINARY_DIR}/external/winflexbision
        )

        execute_process(COMMAND ${CMAKE_COMMAND}
            --install ${CMAKE_BINARY_DIR}/external/winflexbision --prefix ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
        )

        set(BISON_ROOT_DIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" CACHE STRING "BISON_ROOT_DIR" FORCE)
        set(FLEX_ROOT_DIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" CACHE STRING "FLEX_ROOT_DIR" FORCE)

        set(BISON_EXECUTABLE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/win_bison.exe" CACHE STRING "BISON_EXECUTABLE" FORCE)
        set(BISON_version_result "0" CACHE STRING "BISON_version_result" FORCE)
        set(BISON_version_output "bison++ Version 1,0,0" CACHE STRING "BISON_version_result" FORCE)

        set(FLEX_EXECUTABLE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/win_flex.exe" CACHE STRING "FLEX_EXECUTABLE" FORCE)
        set(FLEX_version_result "0" CACHE STRING "FLEX_version_result" FORCE)
        set(FLEX_FIND_REQUIRED "0" CACHE STRING "FLEX_FIND_REQUIRED" FORCE)

        include(UseBISON)
        include(UseFLEX)
    endif()

    # unistd_h
    FetchContent_Declare(unistd_h
        GIT_REPOSITORY https://github.com/win32ports/unistd_h.git
        GIT_TAG 0dfc48c1bc67fa27b02478eefe0443b8d2750cc2)

    FetchContent_GetProperties(unistd_h)
    if(NOT unistd_h_POPULATED)
        FetchContent_Populate(unistd_h)
        # add_subdirectory(${unistd_h_SOURCE_DIR} ${unistd_h_BINARY_DIR} EXCLUDE_FROM_ALL)
        include_directories(${unistd_h_SOURCE_DIR})
    endif()
else()
    if(APPLE)
        find_program(MAC_HBREW_BIN brew)

        if(MAC_HBREW_BIN)
            execute_process(COMMAND ${MAC_HBREW_BIN} "--prefix" OUTPUT_VARIABLE BREW_PREFIX OUTPUT_STRIP_TRAILING_WHITESPACE)
            list(INSERT CMAKE_PREFIX_PATH 0 ${BREW_PREFIX})
        endif()

        execute_process(
            COMMAND ${MAC_HBREW_BIN} --prefix bison
            RESULT_VARIABLE BREW_BISON
            OUTPUT_VARIABLE BREW_BISON_PREFIX
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
        if(BREW_BISON EQUAL 0 AND EXISTS "${BREW_BISON_PREFIX}")
            message(STATUS "Found Bison keg installed by Homebrew at ${BREW_BISON_PREFIX}")
            set(BISON_EXECUTABLE "${BREW_BISON_PREFIX}/bin/bison")
            list(INSERT CMAKE_PREFIX_PATH 0 "${BREW_BISON_PREFIX}")
        else()
            message(FATAL_ERROR "Cannot find bison from homebrew.")
        endif()

        execute_process(
            COMMAND ${MAC_HBREW_BIN} --prefix flex
            RESULT_VARIABLE BREW_FLEX
            OUTPUT_VARIABLE BREW_FLEX_PREFIX
            OUTPUT_STRIP_TRAILING_WHITESPACE
        )
        if(BREW_FLEX EQUAL 0 AND EXISTS "${BREW_FLEX_PREFIX}")
            message(STATUS "Found Flex keg installed by Homebrew at ${BREW_FLEX_PREFIX}")
            set(FLEX_EXECUTABLE "${BREW_FLEX_PREFIX}/bin/flex")
            list(INSERT CMAKE_PREFIX_PATH 0 "${BREW_FLEX_PREFIX}")
        else()
            message(FATAL_ERROR "Cannot find flex from homebrew.")
        endif()
    endif()

    find_package(BISON REQUIRED)
    find_package(FLEX REQUIRED)
endif()

...

BISON_TARGET(xy_parser syntactic.y ${CMAKE_CURRENT_BINARY_DIR}/syntactic.cpp)
FLEX_TARGET(xy_scanner lexical.l  ${CMAKE_CURRENT_BINARY_DIR}/lexical.cpp)
ADD_FLEX_BISON_DEPENDENCY(xy_scanner xy_parser)

add_library(xy-compiler-parser STATIC
    ${SRC}
    ${BISON_xy_parser_OUTPUTS}
    ${FLEX_xy_scanner_OUTPUTS})

target_link_libraries(xy-compiler-parser 
    PUBLIC ${llvm_libs}
    PUBLIC ${FLEX_LIBRARIES})

target_include_directories(xy-compiler-parser
    PUBLIC ${CMAKE_CURRENT_BINARY_DIR}
    PUBLIC ${CMAKE_CURRENT_LIST_DIR})

if(WIN32)
    add_dependencies(xy-compiler-parser
        win_bison
        win_flex)
endif()

Note:

  • target_link_libraries is important because this will automatically add the include directory of flex into the include directories list of this target so that FlexLexer.h can be found when compiling this target.
  • The trick is that an external CMake based project repo can be added as an dependency using the FetchContent module. So the whole WinFlexBison project is included, and the Unistd patch is also included. After the project is fetched, the build and installation command can be called so that the WinFlex and WinBison can be installed into the output directory of the current project. Then the bison and flex executable and header file paths can be inferred and directly injected to override the default actions in the FindFlex and FindBison module bundled with the CMake.
  • The modification in the PR #79 contains the libfl and liby which are required by some projects, so that ${FLEX_LIBRARIES} will work as expected.
  • By the way, the PR #79 only applies to the 3.x branch. For the 2.x branch, you can use the 2.x branch of my fork.

from winflexbison.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.