Set MSBuild properties on Build target input items based on metadata - msbuild

I'm using a code-generation tool (Entity Framework, in this case) in one of my projects, and it generates code that causes the compiler to emit warnings. I'd like to ignore those warnings for a particular set of files. My first thought was that I might be able to set up an ItemGroup to set per-file properties for the compiler, something semantically like this:
<ItemGroup>
<Files Include="Migrations/**/*.cs">
<Properties>
<DisabledWarnings>CS12345;CS4321</DisabledWarnings>
</Properties>
</Files>
</ItemGroup>
I recognize that this isn't valid MSBuild syntax, but it expresses the essence of what I'd like to do.
This question seems somewhat related: Using Item functions on metadata values
Is there a way to do this?

You can't use MSBuild to disable compiler warnings per file. Instead, you need to use #pragma statements in the code.
For example, lets say you wan to disable warnings about usage of obsolete APIs (build warning CS0618). You can surround the code like this to suppress the warning.
#pragma warning disable CS0618
public void Method()
{
new Class1().CallSomeObsoleteMessage();
}
#pragma warning restore CS0618
Alternatively, you can disable warnings for an entire project.
<PropertyGroup>
<NoWarn>$(NoWarn);CS0618</NoWarn>
</PropertyGroup>

Related

In CMake, is there a way to set properties on all target dependencies?

In CMake, we can set target properties as either PRIVATE, PUBLIC, or INTERFACE. Both PUBLIC and INTERFACE properties are inherited by any targets that depend on the current target. However, unless I'm missing something, there doesn't seem to be an easy way to define a property that must propagate in the other direction (i.e., inherited by dependencies of the current target).
Most linkers/compilers require that all linked targets have the same value for certain properties (e.g., the exception handling model). If we want to change one of these properties for an executable it requires that it be set on all of its dependencies. Often these dependencies are submodules in our code where we can't modify their CMakeLists.txt files for our specific use-case. This leaves us with two options:
Set a global property (e.g., CMAKE_CXX_FLAGS or add_compile_options) that propagates to all targets in any subdirectories regardless of whether they are dependencies or not.
Explicitly set the properties on each dependent target using target_compile_options. This gets excessive and repetitive depending on the number of dependencies.
It would be nice if there was a functionality that would pass properties down only to dependency targets without having to specify them all individually. Does anyone know how to do this?
For the case of compiler flags that must be consistent for an entire program (including parts that are dynamically linked), such as MSVC's exception handling model, I think the set-something-global approach is suitable. To me, it seems pragmatic and slightly more robust than adding flags to each third-party target one-by-one (ie. what if you forget to handle to one? or what if third-party targets are added or removed in a new version? it seems like a ripe opportunity for human error).
Setting the environment variable [CMAKE_<LANG>_FLAGS] is a good start. You may need to do more if you are building external projects via ExternalProject.
A word of caution for such settings like the exception handling model: You might be tempted to hardcode this global setting inthe CMake files for your project. If your project is used by people other than just you or your company, and especially if its main component is a library and not an executable, it's good practice not to do that. Don't take away your user's ability to choose something like this (unless for some reason your library requires a certain exception handling model, in which case I would still leave this global setting up to them to set, provide documentation stating this, and look into emitting a CMake warning if a user doesn't comply). Instead, use a feature like CMake presets, or only set it if the project is the top-level project
An intersting side note: CMake currently globally "hard-codes" /EHsc for MSVC builds by default. Here's the ticket discussing this

Using GroovyDSL with #TypeChecked in IntelliJ IDEA: Build Project fails

I have a jenkins.gdsl file defining some bindings I'm using in my Groovy script. In addition, I'd like to use the #TypeChecked annotation on my methods to get some guarantees about built code.
My jenkins.gdsl file looks like:
contributor(context(scope: scriptScope())) {
// some definitions
}
And then my script.groovy looks like:
#TypeChecked(extensions='jenkins.gdsl')
void doStuff() {
// ...
}
IntelliJ IDEA autocomplete works, but when building my project I get an error in my jenkins.gdsl file:
Error:Groovyc: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport.scriptScope() is applicable for argument types: () values: []
Removing (extensions='jenkins.gdsl') gets rid of this error, but then I lose my GDSL definitions when building, so that's a no-go.
It feels like the solution would involve bringing in IntelliJ's standardDsls. I am not at all sure how to do this, or whether it is in fact the correct approach.
#TypeChecked is a Groovy compiler annotation that can run some code during compilation.
But gdsl is an IntelliJ IDEA-specific script that's used only by the IDE to provide some completion and other coding assistance. It doesn't have anything in common with the compiler, and neither of those know anything of each other. So you can remove the extensions value, as it won't provide any typechecking during compilation.

CMake generator expression dependent on source property

I'm trying to add a compiler flag to all source files that don't have a specific property set.
The first use case is adding -Wshadow -Wuseless-cast to the command line for all non-GENERATED files, later on I'd like to add custom properties for other compiler flags.
I'd like to avoid the "trick" of countermanding default compile flags with per-source flags, because that requires subdirectory CMakeLists.txt to be aware of different options, the need to override them and the proper way to extend a list of flags with
set_property(
SOURCE ...
APPEND_STRING
PROPERTY COMPILE_FLAGS "-Wno-shadow ")
which IMO is a lot of boilerplate that needs to be duplicated often.
The documentation for generator expressions is awfully silent on checking source properties. Can this be done at all?

Setting DefineConstants dynamically via MSBuild import

I have an .msbuild import that defines a property group containing preprocessor definitions, among other things:
<PropertyGroup>
<DefineConstants>$(DefineConstants);MY_CONSTANT_VALUE</DefineConstants>
</PropertyGroup>
This is <import>ed into both csproj and vcxproj files. At build time, the C# preprocessor appears not to have visibility of the defined constants. However, an equivalent C++ preprocessor definition in the same file works correctly, and the constants are discovered:
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>$(PreProcessorDefinitions);MY_CONSTANT_VALUE</PreprocessorDefinitions>
</ClCompile>
<ItemDefinitionGroup>
Any ideas why this only works in C++?
Default property group declaration doesn't cater about previously existed properties and defined like this:
<DefineConstants>TRACE</DefineConstants>
So make sure you are importing your .msbuild file at the end of your .csproj.vbproj, not in the beginning.
If this doesn't help - can you provide a log of msbuild with diagnostic level of verbosity and post link here.

Add no-objc-arc to the implementation or interface file

I have a class (NDTrie on github) which uses c struct for its internal structure, it would make it easier for users to use it in their projects with automatic reference counting by adding the fno-objc-arc to the source file instead of requiring users to set it in the build phase for that source file, is there a way to do that.
Per-file compiler flags are tacked onto files on a per-project basis (files meant to be compiled down to some form of machine code generally try to avoid metadata). If you'd like to specify the flag and have it update the required field in any Xcode project, you can use CocoaPods to write a pod file for your dependancy. Let the underlying tool handle the rest.
No, it's not possible to separate portions of translations based on features and alter their flags for that translation in this case.
You should approach it from another angle. The ARC feature treats all preprocessed input as having ARC enabled or not, based on the compiler flags of the current translation.
The most obvious workarounds:
0) Impact reduction: Hide the implementation, where possible
1) Translation specific conditions: Use __has_feature(objc_arc) to determine whether you are or are not dealing with an ARC-enabled translation -- if you are using clang, the expression __has_feature(objc_arc) expands to 1 when ARC is enabled. Then you can conditionally make portions of your program visible or annotate it differently, depending on whether or not ARC is enabled.
2) Detect and fail: In some remaining cases, you may opt for:
// >> detect clang or GCC if needed
#if __has_feature(objc_arc)
#error This file cannot be compiled with ARC enabled (+HYPERLINK so you don't get a flooded inbox)
#endif
// << detect clang or GCC if needed