Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmake remove circular linking and reorganize #9292

Merged
merged 2 commits into from
Apr 30, 2018
Merged

Conversation

dagar
Copy link
Member

@dagar dagar commented Apr 12, 2018

PX4 is supposed to be modular, but because of the current cmake build system everything is built and linked together as a large pile of code. The lines between different modules and different layers of the system have become blurred in many cases.

This PR changes the PX4 cmake build slightly to allow us to actually enforce and maintain modularity. We can then tease apart some of these interconnected components and work towards more well defined layers (#8784 (comment)).

First instead of calling everything a module (px4_add_module) and linking it all together (with multiple passes to resolve circular dependencies) I changed px4_add_module to require a MAIN. Any modules included in a configuration (eg nuttx_px4fmu-v2_default.cmake) are included in the final PX4 binary. Modules without a main are now simply libraries (px4_add_module -> px4_add_library). Some libraries are part of PX4 and need access to uORB, the parameter system, logging system, etc (PX4 libraries: px4_add_library), but many don't (regular cmake: add_library).

Libraries aren't automatically included (linked) in your final PX4 binary, they must be added as a module dependency. As a result we don't need to list them per configuration. Including a particular module pulls in a library only if it's needed as a dependency. With that dependency comes additional include directories and build flags if specified by the library.

PX4 CMake Changes

px4_add_module (PX4 Modules)

  • now requires MAIN
  • access to uORB, params, and other libraries
  • name is defined for the logging system (PX4_INFO, PX4_WARN, PX4_ERR)
  • no access to other modules by default

px4_add_library (PX4 libraries)

  • access to PX4 uORB, params, etc
  • not linked to final binary by default, must be a dependency (DEPENDS) of a module
  • name is defined for the logging system (PX4_INFO, PX4_WARN, PX4_ERR)
  • use INTERFACE to specify include directories, build flags, etc that should be used by the module
  • can use other libraries (PX4 or not)

add_library

  • use for regular libraries that shouldn't have access to anything PX4 specific (uORB, params, etc)
  • can't use logging system (PX4_INFO, PX4_WARN, PX4_ERR)
  • not linked to final binary by default, must be a dependency (DEPENDS) of a module

cmake configurations

  • all libraries (src/lib) are automatically included in every build

  • only the ones that are dependencies of PX4 modules (px4_add_module DEPENDS) are actually built

  • other common pieces (eg src/drivers/boards/${BOARD}) are also included automatically now

  • this removes the need for every cmake configurations to exhaustively list every component and core system piece. Modules are specified directly, and then the libraries are pulled in as dependencies.

  • example, here's how the px4fmu-v2 configuration (nuttx_px4fmu-v2_default.cmake) is simplified.

    • image

This also removes the need to link with --start-group/--end-group to resolve the circular dependencies.

Notes

@dagar dagar force-pushed the pr-cmake_lib_module branch 6 times, most recently from 9d002bd to a026f9e Compare April 12, 2018 06:04
@dagar dagar changed the title [WIP] cmake remove circular linking and reorganize cmake remove circular linking and reorganize Apr 12, 2018
@dagar dagar force-pushed the pr-cmake_lib_module branch 5 times, most recently from 422a415 to 66161e9 Compare April 14, 2018 15:03
@dagar
Copy link
Member Author

dagar commented Apr 14, 2018

@Stifael you might be interested in some of this. Once this is in PX4 we can update ECL with something more modular that's also a proper standalone cmake library.

CMakeLists.txt Outdated
set(PX4_BUILD_TYPE "MinSizeRel")
elseif (${OS} STREQUAL "bebop")
set(PX4_BUILD_TYPE "MinSizeRel")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is depreciated?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually just wrong. ${OS} is never set to bebop.

@@ -38,6 +38,5 @@ px4_add_module(
SRCS
tune_control.cpp
DEPENDS
platforms__common
tunes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so much better

@@ -53,6 +53,8 @@ if (NOT ${OS} STREQUAL "qurt")
simulator_mavlink.cpp)
endif()

add_subdirectory(ledsim)

px4_add_module(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to free ecl a from px4 dependencies such that it could be included similar to the simulator

@Stifael
Copy link
Contributor

Stifael commented Apr 16, 2018

this makes the px4 cmake structure much more understandable

1 similar comment
@Stifael
Copy link
Contributor

Stifael commented Apr 16, 2018

this makes the px4 cmake structure much more understandable

@dagar
Copy link
Member Author

dagar commented Apr 17, 2018

Here's another good summary of modern cmake. https://gist.github.com/mbinna/c61dbb39bca0e4fb7d1f73b0d66a4fd1

@dagar dagar force-pushed the pr-cmake_lib_module branch 2 times, most recently from 8bacecc to b06b977 Compare April 17, 2018 14:17
@dagar dagar requested a review from bkueng April 17, 2018 14:17
@dagar dagar force-pushed the pr-cmake_lib_module branch 2 times, most recently from 4005167 to 27debf9 Compare April 20, 2018 21:44
 - px4_add_module now requires MAIN
 - px4_add_library doesn't automatically link
 - set BOARD_NUMBER_BRICKS to 0 for boards without analog power bricks
Copy link
Member

@bkueng bkueng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good as far as I can tell (proper cmake has remained a mystery to me so far).

There's an incremental build issue when removing an aiframe file & from CMakeLists.txt, you get:

ninja: error: '../../ROMFS/px4fmu_common/init.d/4040_reaper', needed by 'genromfs/px4fmu_common/init.d/rcS', missing and no known rule to make it

But it already exists on master.

Let's make sure get it in soon.

# vim: set noet ft=cmake fenc=utf-8 ff=unix :
controllib
flight_tasks
git_ecl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are both git_ecl and ecl needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment yes. The git_ecl target is on the Firmware side and ensures the submodule is actually in place. Then ecl is the actual library in the ecl project. Mixing submodule dependencies into the build system like this is a bit unusual, but has worked surprisingly well.

Later we can split the ecl project into separate libs for ekf, tecs, etc that build standalone or within PX4.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it's just not obvious for someone adding a new module

@dagar
Copy link
Member Author

dagar commented Apr 25, 2018

Thanks for the review. Can you open an issue about any incremental build issues you hit?

@bkueng
Copy link
Member

bkueng commented Apr 25, 2018

Can you open an issue about any incremental build issues you hit?

#9369

@dagar dagar merged commit 8404889 into master Apr 30, 2018
@dagar dagar deleted the pr-cmake_lib_module branch April 30, 2018 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants