absl-py save and ignore unrecongized flags - absl-py

Sometimes I have to pass flags to the python script such as --local_rank in addition to the prespecified ones in absl-py. Is there a way to -
ignore unspecified flags
save pre- and un-specified flags in json as dictionary

Related

CMake: how to embed build configuration (Debug, Release, MinSizeRel,...) into binary?

I'm using Cmake's configure_file() function to generate a header that I include into my project.
This way, I can for example store into the resulting binary the git commit hash of the code beeing compiled.
Now I'm trying to make my program aware of the compilation configuration which has been used: Debug, Release, MinSizeRel or RelWithDebInfo.
The toolchain used is VisualStudio, therefore all configurations are generated by CMake at the same time. So for example, CMAKE_BUILD_TYPE is alays set to Release.
What is the common way to make the program built aware of the the build mode used ?
You may create a file, containing build type-specific values, using file(GENERATE) command. This command expands generator expressions.
For example, the call
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/build_$<CONFIG>
CONTENT "Build type: $<CONFIG>")
at the end of configuration will create build_Debug file with content Build type: Debug, build_Release file with content Build type: Release and so on.
Resulted files could be used in custom commands:
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/some_file_$<CONFIG>
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/build_$<CONFIG>
COMMAND do-something-with build_$<CONFIG>
)
Note, that file(GENERATE) doesn't expand variables. So if you want to combine in the resulted file both variables values and generator expressions, then you need to "pipe" configure_file with file(GENERATE).
my_file.in:
var: #MY_VAR#
build type: $<CONFIG>
CMakeLists.txt:
# Creates intermediate file with expanded variables.
configure_file(my_file.in my_file_intermediate #ONLY)
# Expands generator expressions in the intermediate file and creates "final" files
file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/my_file_$<CONFIG>
INPUT ${CMAKE_CURRENT_BINARY_DIR}/my_file_intermediate)
You can use CMake generator expressions with the add_compile_definitions command (or any other command which accepts generator expressions).
Like this: add_compile_definitions(CONFIG_TYPE="$<CONFIG>"). Then use the CONFIG_TYPE macro in your C++ code.

Is there a method to make `gcc' dump/display all the flags in use while compiling code?

Do note this is different from
Get the compiler options from a compiled executable? which I did go through in detail.
Although -frecord-gcc-switches is great, it only captures the command line arguments.
For example, I am not interested in capturing -O2 which is usually passed in command line. I am more curious about recording all the flags like -fauto-inc-dec which are enabled by -O2.
(In contrast to the link above, do note that I have access to the source, the compiler and the build infrastructure. I just want to capture the flags during compilation. Not picky about any specific gcc version)
You can try -fverbose-asm. That dumps the optimisation options used in a comment at the top of the assembly file.

Running GCC preprocessor -fdirectives-only *without* implicit -dD

Basically, I want to do what the title says.
I have some mildly complex header files, and I want to generate a single header file I can release as the public interface of a DLL.
I'm generating the header file as follows:
${CMAKE_CXX_COMPILER} ${CMAKE_CXX_FLAGS} -fdirectives-only -P \
-E ${CMAKE_CURRENT_SOURCE_DIR}/dll_header_base.h -o generated_public_header.h
If I run the compiler without -fdirectives-only, the precompiler is much more aggressive about stripping out contents of the header. The output of -fdirectives-only is almost what I want, but that particular flag forces the -dD flag on as well, which prepends all the compiler-defined macros to the generated header (450 lines of them!).
I don't really understand why -fdirectives-only forces -dD in the first place, and for my case, the later flag basically makes it useless. Looking at the options for -dLETTER, there also seems to be no way to turn it off.
How do I extract the directives-only preprocessed output without all the additional cruft?
Yes, I could pretty easily solve this with command line tools, but I want to keep the solution entirely constrained to the compiler/CMAKE. Eventually, I'd like to also support building on windows.

Substituting for a .SET on the the command line

I have some (Microblaze) assembly I need to build (via the GCC cross-assembler and linker) and execute many times with the (same) constants, currently fixed via
.SET
commands, changed each time.
Is there a way to automate the setting of in-assembly constants in this way and so avoid the dull task of resetting the code for each build?
You can use the power of C pre-processor in your assembler files. This could be done simply changing file extension from .s to .S (capital S) on Unix-like platform or to .sx on Windows. Then using gcc instead of gas over these files will let C pre-processor first run through the source and then gas will be called automatically.
In this case you can use all regular pre-processor #define, #ifdef, etc. And of cause you can pass these defines from the command line with gcc's -D parameter.

Confused about CMake's cached variable setting priority

I'm confused about CMake's cached variables:
Case 1: CACHE + FORCE
set(CMAKE_CXX_FLAGS "myflags" CACHE STRING "" FORCE)
First CMake run: "myflags" appears in the CMakeCache.txt file as intended.
Command line options: command line options do not override "myflags" - seems like FORCE has higher priority than command line -D...="..." arguments. This is not desired - I would like command line arguments to override "myflags".
Case 2: only CACHE
set(CMAKE_CXX_FLAGS "myflags" CACHE STRING "")
First CMake run: nothing appears in the CMakeCache.txt file. I want"myflags" to appear for the first run.
Command line options: command line have priority over "myflags".
Am I correct about my conclusions? Or do "default variables" such as CMAKE_CXX_FLAGS behave differently?
Is there a way to have "myflags" written in the CMakeCache.txt file during the first CMake run (when CMake wasn't run previously in this folder)?
I'd like to set "myflags" during the first CMake run in the cache, then allow the user to override it using the command line.
If I use FORCE, the user can't override it via command line.
If I don't use FORCE, "myflags" isn't written in the cache file during the first run.
This is consistent with the behaviour explained in the documentation:
Normally, set(...CACHE...) creates cache variables, but does not modify them. If FORCE is specified, the value of the cache variable is set, even if the variable is already in the cache. This should normally be avoided, as it will remove any changes to the cache variable’s value by the user.
CMAKE_CXX_FLAGS is present in the cache even without set(CMAKE_CXX_FLAGS ... CACHE ...) in your CMakeLists.txt, because CMAKE_CXX_FLAGS variable is already set during compiler flags initialization.

Resources