HEP-SoC / SoCMake

CMake based hardware build system
https://hep-soc.github.io/SoCMake/
GNU Lesser General Public License v3.0
8 stars 1 forks source link

Use generator expressions for conditional flags #44

Open Risto97 opened 2 months ago

Risto97 commented 2 months ago

Generator expressions can be used to simplify some of the backend functions.

For example if a function has following optional arguments:

    cmake_parse_arguments(ARG "XML;FILE_LIST" "TOP_MODULE" "" ${ARGN})

Instead of doing

    if(ARG_FILE_LIST)
        set(FILE_LIST_ARG --module-files)
    endif()

    set(__CMD ${VHIER_EXECUTABLE}
        ${FILE_LIST_ARG}
    )

We can do this:

    set(__CMD ${VHIER_EXECUTABLE}
        $<$<BOOL:${ARG_FILE_LIST}>:--module-files>
    )

These expressions can be more complex generator expressions

mksoc commented 2 months ago

I tried to use it in copy_rtl_files.cmake like so, but for some reason it doesn't work and returns always empty strings (I'm doing something wrong for sure).

    set(__CMD ${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/copy_rtl_files.py
        $<$<BOOL:${ARG_TOP_MODULE}>:--top-module ${ARG_TOP_MODULE}>
        $<$<BOOL:${ARG_SKIPLIST_FILE}>:--skiplist ${ARG_SKIPLIST_FILE}>
        $<$<BOOL:${ARG_SYNTHESIS}>:--synthesis>
        --deps_dir ${FETCHCONTENT_BASE_DIR}
        ${INCDIR_ARG}
        $<IF:$<BOOL:${ARG_OUTDIR}>,--outdir ${ARG_OUTDIR},--outdir ${CMAKE_BINARY_DIR}/ip_sources>
        ${RTL_SOURCES}
    )
Risto97 commented 1 month ago

Hello @mksoc

I was playing a bit more with the generator expressions, the issue seems to be having whitespaces in the generator expressions. It is explained here: https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#whitespace-and-quoting

To summarise for your example, you need to:

Your command would look like this:

    set(__CMD ${Python3_EXECUTABLE} ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/copy_rtl_files.py
        "$<$<BOOL:${ARG_TOP_MODULE}>:--top-module\;${ARG_TOP_MODULE}>"
        "$<$<BOOL:${ARG_SKIPLIST_FILE}>:--skiplist\;${ARG_SKIPLIST_FILE}>"
        "$<$<BOOL:${ARG_SYNTHESIS}>:--synthesis>"
        --deps_dir ${FETCHCONTENT_BASE_DIR}
        ${INCDIR_ARG}
        "$<IF:$<BOOL:${ARG_OUTDIR}>,--outdir\;${ARG_OUTDIR},--outdir\;${CMAKE_BINARY_DIR}/ip_sources>"
        ${RTL_SOURCES}
    )

Also keep in mind that the the generator expressions are evaluated at the generate phase, after the configure phase. This means that if you use it for OUTDIR variable, and later if you build a file path using that variable (file generated by a command), in the _SOURCES of the IP there will be a generator expression at configure time, which might be a bit annoying. I would maybe not use generator expression for OUTDIR or anything that influences file paths, at this point.

You can decide if you think its worth using them for simple CLI options, I am not sure at this point its better than having an if() clause.

P.S. for OUTDIR, I will most certainly add a macro that will deal with that so it reduces a bit the code in backend functions, as OUTDIR will always be set the same way