Extract compiler and linker flags from Bazel - bazel

I need to extract the compiler flags and linker flags for a certain application that is built by Bazel.
Is there a specific command to do so?

bazel clean # force bazel to rebuild everything
bazel build -s //...
There are more advanced approaches, e.g.:
kythe
Hedron's Compile Commands Extractor for Bazel
https://github.com/vincent-picaud/Bazel_and_CompileCommands
You can also use system tools such as strace to figure how what tools are called by Bazel and which command line parameters are involved.

Related

Xcode build phases: Use previous build warning output in "run script" phase

I want to have a run script that looks at the number of previous warnings (probably during the compile phase), and generates an error which stops the run/buid if the number of warnings is too large.
I am working on an app that has a large amount of warnings that happen during compile. They do not stop the app from compiling, but I want to stop devs from adding more warnings as they add features.
I have looked everywhere, but so far have not found a way to use previous build output in the run script phase of the build. Is there any way of doing this? Some kind of env variable, or a way of monitoring the build output in a custom script as it happens?
I see a similar question here:
Is there a way to access Xcodes current build output from a build phase run script?
But it seems that the output is only available after the build is complete in that answer. Is there any other option that would allow the build to be failed before it finishes?
Any insight into the build system in Xcode would be appreciated! Cheers
Additional context:
All I need is for Xcode's default warnings to have a feature similar to SwiftLint's warning_threshold
https://stackoverflow.com/a/52256459/7623867
I have looked into that problem as well for quite some time (I wanted to use warnings for quality measurement). The only way I have found is to compile the whole project with xcodebuild, with a command similar to:
xcrun xcodebuild -project MyProject.xcodeproj/ -scheme MyScheme -destination "platform=iOS Simulator" build -quiet | tee xcodebuild.log
and then counting the warnings using AWK or some other tool.
Here is an example.

Are there any tools for migrating old bazel workspace into latest version bazel

I have an existing bazel project which can be compiled successfully by bazel v0.5.3. Since the plugin for clion only support bazel 0.24.0+ , I update my bazel into the latest version. However clion tells
"ERROR: Error evaluating WORKSPACE file", when loading the project into clion.
Are there any approach to update old bazel project?
Bazelisk provides a --strict and --migrate flag that can help you:
USE_BAZEL_VERSION=0.24.0 bazelisk --strict build //...
--strict enables all incompatible flags and helps you to identify upgrade problems.
If --strict fails you should try --migrate. It enables incompatible flags step by step and gives you a report of the incompatible flags that lead to a build problem:
USE_BAZEL_VERSION=0.24.0 bazelisk --migrate build //...
Take also a look at the documentation about Backward Compatibility:
When we introduce an incompatible change, we try to make it easier for
Bazel users to update their code. We do this by means of migration
windows and migration recipes.
Migration window is one or more release of Bazel during which a
migration from old functionality to new functionality is possible,
according to a migration recipe.
During the migration window, both the old functionality and the new
functionality are available in the Bazel release. For every
incompatible change, we provide a migration recipe that allows
updating the user code (BUILD and .bzl files, as well as any Bazel
usage in scripts, usage of Bazel API and so on) in such a way that it
works simultaneously without any flags with old and new functionality.

Bazel Complains about Dependencies to system header files

Our Bazel build complains as
ERROR: XXX/BUILD:5:1: undeclared inclusion(s) in rule 'YYY':
this rule is missing dependency declarations for the following files included by 'ZZZ.c':
'/usr/lib/gcc/x86_64-linux-gnu/6/include/stddef.h'
'/usr/lib/gcc/x86_64-linux-gnu/6/include/stdint.h'.
Why does it complain about dependencies to gcc system header files?
I'm using Ubuntu 17.04 with GCC 6.3.0.
Do you use custom crosstool? If so, can you check that those directories are covered by cxx_builtin_include_directory messages?
If you use builtin Bazel autoconfiguration, can you make sure Bazel is not using some other gcc? Bazel calls cc -E -xc++ - -v to find all the builtin directories, do you expect this to work? Also, Bazel checks whether CC environment variable is set, and if yes, it will take the compiler from there.
I solved it by cleaning up Bazels temporary files.
The reason was an upgrade of GCC during Bazel development.

Running clang static checker at project level (iOS app) rather than file by file (xcodebuild)

After solving this Omitted code blocks from clang AST for ObjectiveC, I've tried it on a small Objective C .m file along with an appropriate compile_commands.json and it works properly and I get the entire syntax tree.
Now I'm trying to see if it's possible to run it on the entire xcodebuild
[
{
"directory" : "/Users/xx/Desktop/iOSApplication",
"command" : "xcodebuild clean build CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO -project /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj/",
"file" : "iOSApplication.xcodeproj"
}
]
When I tried to then run clang-check -ast-dump /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj it gives me the errors error: unknown argument: '-project' and error: unable to handle compilation, expected exactly one compiler job in ''
Is it actually possible to run the AST based checker on the entire xcodeproject? Or how should I go about compiling the files 1 at a time?
I've managed to generate the compile_commands.json by following this guide here http://docs.oclint.org/en/stable/guide/xcodebuild.html
However, I'd still like to be able to run my RecursiveASTVisitor on the entire projects. Or alternatively, pass in the xcode project and enumerate all the source files would probably work too.
Anyone has ideas how to go about passing entire xcodebuild project as parameter for RecursiveASTVisitor?
Right now I'm running my ASTVisitor like this ./MyASTChecker ~/Desktop/directory/sample1.m but I'd like to make it do something like ./MyASTChecker ~/Desktop/directory/sampleproject.xcodeproj
The way I do it is at compile time using scan-build. This works for me with cmake/make based projects.
scan-build --use-analyzer=clang -enable-checker <checker_name> make
This will read the makefile and build everything in there while running the specified checker on each file as it's compiled. If you only want to build with some flags or a specific target, you can do this:
scan-build --use-analyzer=clang -enable-checker <checker_name> make <build_options>
If you instead have a cmake based project. You might first want to generate a makefile in a build directory. I do this for that:
cmake <path_to_cmakelists> -DCMAKE_CXX_COMPILER=c++-analyzer -DCMAKE_CC_COMPILER=ccc-analyzer
This followed by scan-build from above will compile and run checker on the source files while each file is being compiled.
I have only tried this with CMAKE / MAKE but should work with xcode like this:
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild
and with build options
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild <build_options>
You can read more about scan-build here

I want to know how to make a makefile for iOS "fat" library

I want to create a (non-xcode) makefile to create a fat library (emulator + device(s)) that can be imported into an XCode project using a makefile that calls the basic command line tools directly (not running XCODE from the command line, but the MAC Gcc and it's related utilities) - this is for .m, .mm, .c, and .cpp source files.
Ideal would be to find an example that works for a simple library (not by calling a makefile generator that makes an almost non human readable makefile)
anyway anyone know of such a thing or appropriate mechanism for doing the same?
Also an ability to extract the complier flags from an XCode project would be real handy :)
The purpose is I want to add a module to my cross platform libraries so I can integrate them into an iOS project.
Thanks!!
You can extract the compiler flags by viewing the build details or, more simply, running xcodebuild from the command line.
To create a fat binary, you either take advantage of the compiler toolchain's built-in support on the Mac OS X platform by passing multiple -arch arguments, like so:
clang -arch i386 -arch x86_64 -framework Foundation simple.m -o simple
Alternatively, you build the binary once for each desired architecture, then wrap all those binaries into a single fat binary using lipo. This is handy when working with ported Unix software; just change the build result directory each time, then smash them all together after building with lipo. Assuming you have simple-i386 and simple-x86_64, you would then do:
lipo simple-i386 simple-x86_64 -create -output simple
This would create a fat binary named simple containing simple-i386 and simple-x86_64.
Ok - I found this which is a great HOWTO o building a fat library using XCODE that outlines the process and how to create the projects
http://blog.boreal-kiss.net/2011/03/15/how-to-create-universal-static-libraries-on-xcode-4/
being a newbie to XCode and iOS development I had to discover a few things.
you can view the actual command line output of a build to see what the gcc flags are.
View->Navigators->Log - then control click on the messages list to "expand all Transcripts"
to see what stdout and stderr from the chosen build's build output.
You can execute an "external build tool" with your .bashrc and .bash_profile environment settings by making the command and arguments a login shell: "bash --login -c 'mybuildtool [my tools args] $(ACTION)', and thus bypass having to deal with the hard to maintain MacOSX launchd settings etc. this works for things like using ruby and rake as well as make etc.

Resources