Skip to content
This repository was archived by the owner on May 31, 2025. It is now read-only.

Added support for compile options - #844

Closed
mathias-luedtke wants to merge 14 commits into
ros:kinetic-develfrom
mathias-luedtke:compile-options
Closed

mathias-luedtke wants to merge 14 commits into
ros:kinetic-develfrom
mathias-luedtke:compile-options

Conversation

@mathias-luedtke

@mathias-luedtke mathias-luedtke commented Jan 8, 2017 •

Copy link
Copy Markdown
Contributor

After the discussions in #610 I have now refactored and improved #842

  • introduces <comp>_COMPILE_OPTIONS and catkin_COMPILE_OPTIONS
  • all entries get set-filtered (stable) with warnings for colliding entries ( key vs. key= vs. key=val1 vs. key=val2)
  • <comp>_COMPILE_OPTIONS will be passed to CMake config in catkin_package
  • added COMPILE_OPTIONS option to catkin_package

I made the decision to switch to compile options completely for the following reasons:

  • add_compile_options can handle all *_DEFINITIONS settings
  • users would need to call add_compile_options and add_definitions
  • duplicates and collisions would have to be checked in compile options and definitions

Open issues:

  • How to properly add test/test_list_append_keys_from.cmake to catkin tests? It prints CMake warnings..
  • Not sure if the DEFINES option is really needed. In most cases <comp>_COMPILE_OPTIONS might be more reasonable, since it can be passed to the exporting package easily (see set_compile_options example package)
  • documentation is not complete

Comment thread cmake/catkin_package.cmake Outdated
# LIBRARIES projlib1 projlib2
# CATKIN_DEPENDS roscpp
# DEPENDS Eigen
# DEFINES MYDEFINE=1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be named COMPILE_OPTIONS to match the underlying variable as well as used CMake function? Then defines would need to be passed as -Dkey=value but it allows to pass anything else too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These are not compile options, but definitions. -D is added automatically: https://github.com/ipa-mdl/catkin/blob/compile-options/cmake/catkin_package.cmake#L385
(I have adopted the syntax from #610)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I saw that. But internally they are stored as compile options. Why should the user not be able to pass compile options which are not definitions through catkin_package?

Comment thread cmake/catkin_package.cmake Outdated

set(PKG_COMPILE_OPTIONS ${${PROJECT_NAME}_COMPILE_OPTIONS})
separate_arguments(PKG_COMPILE_OPTIONS)
separate_arguments(PROJECT_DEFINES)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this necessary? The argument is already received as a list from cmake_parse_arguments.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wasn't aware of that, this was only a safe guard. If cmake_parse_arguments already takes care of it this line can be omitted.

@dirk-thomas dirk-thomas Jan 25, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In the context of the related discussion I should clarify that I assume the arguments are being passed as a list, like this (same as e.g. DEPENDS):

catkin_package(DEFINED "something" "something-else")

endforeach()

set(PKG_COMPILE_OPTIONS ${${PROJECT_NAME}_COMPILE_OPTIONS})
separate_arguments(PKG_COMPILE_OPTIONS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as for the next line. Why should we allow the user to specify a space separated string if CMake provides support for lists natively?

@mathias-luedtke mathias-luedtke Jan 25, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nothing prevents the user from specifying set(${PROJECT_NAME}_COMPILE_OPTIONS "-Wall -DBla -fomit-frame-pointer").
As testing for unique options is crucial, we need to split the string into single options.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would expect the arguments to be passed separately and not in a single string, e.g.:

set(${PROJECT_NAME}_COMPILE_OPTIONS "-Wall" "-DBla" "-fomit-frame-pointer")

And while setting a global variable like this is possible it is hard to discover. Having a documented argument in catkin_package is imo preferred.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would expect the arguments to be passed separately and not in a single string

I have seen add_definitions("...") in a couple of projects, so people might pass it as full string because they have found it somewhere in a stackoverflow answer.
IMHO if we need to check for conflicting settings then we should split them.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, if we want to support both uses cases that sounds good.

Comment thread cmake/list_append_keys_from.cmake Outdated
break()
elseif(_elem MATCHES ${_expr})
set(_found TRUE)
message(WARNING "could not add '${_item}' (from '${source}'), already contained: '${_elem}'")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This warning would also appear if the two values _item and _elem are identical which doesn't make much sense.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This case should be covered by if(_elem STREQUAL _item).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good point. I missed that condition above. Sorry for the noise.

Comment thread cmake/list_append_keys_from.cmake Outdated
#
# Append key[=value] elements from one list to another list if they are not already in the target list.
#
macro(list_append_keys_from listname source)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please convert this into a function (see #835).

Also passing the source list name as source unnecessarily restricts how this function can be called. I would suggest considering all ARGN (beside listname) to be appended.

@mathias-luedtke mathias-luedtke Jan 25, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't want to make this macro overly complex and spared the outer loop for ARGN since it was not needed for the use case. Its calling convention differs from list_append_unique, because it simplified the calling code.

I am not sure if this can be implemented as a function. I have not found any way to read a named variable that was defined in the parent scope.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The calling code will be almost identical. Either you call list_append_keys_from(targte_list_name source_list_name) or list_append_keys_from(targte_list_name ${source_list_name}). But the second signature also support calling it without having a source list in the first place but just plain items, e.g. list_append_keys_from(targte_list_name "one" "two" "three" ${or_even_another_list}).

The implemenation is very similar to list_append_unique, simply loop over ${ARGN}.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

And I now remember the reason for not passing the elements directly: I wanted to print the source of the conflicting entry in the warning.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would suggest passing an optional keyword argument like CONTEXT for this purpose.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Does an optional CONTEXT variable work with a variable argument list?

@dirk-thomas dirk-thomas Jan 25, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, except that the variable argument list can't contain that specific keyword, so I think CONTEXT would be "safe". It needs to be extracted and used with something like this:

cmake_parse_arguments(ARG "" "CONTEXT" "" ${ARGN})
# ARG_CONTEXT
# ARG_UNPARSED_ARGUMENTS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have not found any way to read a named variable that was defined in the parent scope.

Not sure what I have tested back then. ${${listname}} works like a charm...

Comment thread cmake/list_append_keys_from.cmake Outdated
@@ -0,0 +1,22 @@
#
# Append key[=value] elements from one list to another list if they are not already in the target list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The docblock should document all arguments.

@mathias-luedtke mathias-luedtke Jan 25, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just copied it from list_append_unique and missed adding proper argument documentation, I can add this after we have decided on the parameter list below.

@mathias-luedtke

Copy link
Copy Markdown
Contributor Author

What do you think of using COMPILE_OPTIONS in catkin_package, but supporting {$PROJECT_NAME}_COMPILE_OPTIONS as well?
(Same behaviour as for CFG_EXTRAS and EXPORTED_TARGETS)

@dirk-thomas

Copy link
Copy Markdown
Member

Yes, if the argument is called COMPILE_OPTIONS you should also be able to append to a global var ${PROJECT_NAME}_COMPILE_OPTIONS. This basically allows you to contribute to catkin_package() without controlling the call itself.

In ament_cmake the ament_package() function has no arguments at all. For every kind of argument there is a separate function, e.g. ament_export_libraries(...), which behind the scene just extends the global variable.

@mathias-luedtke
mathias-luedtke force-pushed the compile-options branch 2 times, most recently from f2f30e7 to 42f2088 Compare February 7, 2017 14:13
@mathias-luedtke

Copy link
Copy Markdown
Contributor Author

@dirk-thomas: Thanks for your feedback! I have updated the code accordingly.
I haven't rebase/squashed yet to simplify the review.

In the end it will be reduced to 2-3 commis.

@dirk-thomas dirk-thomas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am sorry for the late response. Sadly there is only very little ROS 1 time. Since a new release of this package is planned for the near future I would like to get this in though. Please see the few inline comment / questions.


set(PKG_COMPILE_OPTIONS ${${PROJECT_NAME}_COMPILE_OPTIONS})
separate_arguments(PKG_COMPILE_OPTIONS)
separate_arguments(PROJECT_COMPILE_OPTIONS)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this is a user provided variable I don't think it is a good idea to mutate it. This should store the modified value in a local variable instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, I will change it.

# :param CONTEXT: context to be printen in case of errors
# :type CONTEXT: string
#
# :param LEVEL: level of the error message

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should mention the default level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed.

@@ -0,0 +1,55 @@
#
# Append key[=value] elements from one list to another list if they are not already in the target list.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the same key is being inserted multiple time with different values shouldn't the last one be used (instead of the first one)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If I recall it correctly, the dependencies will be resolved first.
So changing some options might break ABI or even API.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, I guess that is the problem of trying to set different values (coming from different packages). You already have the same problem with two different values both coming from dependencies because you have little control in which order they are being found.

I think it is just counter intuitive if this function uses the first value.

@mathias-luedtke mathias-luedtke Jul 17, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You already have the same problem with two different values both coming from dependencies because you have little control in which order they are being found.

You have proposed that in this case the dependencies must be resolved separately and cannot use the combined compile options.

I think it is just counter intuitive if this function uses the first value.

For me it's not counter intuitive, it resembles the C++ map::insert logic

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should clarify the goal here first. Then come up with the behavior and name of this function.

The function is being used when calling find_package(catkin REQUIRED COMPONENTS foo bar) where as both components might have (recursive) dependencies themselves. What do you expect to happen if they export different values for the same key and why?

Re-reading some of this thread in the original comment you mentioned that this would be a "collision" and considered to print a warning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What do you expect to happen if they export different values for the same key and why?

As we discussed it should print a warning (in any case).
It might even better to issue a fatal error, as the situation is unresolvable (?).
This way it doesn't matter which comes first or last ;)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since you are proposing the feature I will leave it up to you to decide if it is a warning by default or an error.

From my point of view this new feature will likely not be widely usable for e.g. setting the compiler standard version (which was one reason it was proposed in the first place I think). Simply because any situation where packages don't agree on the value are going to be a problem.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right now I don't know if we should merge it.
It adds complexity, but might lead to problems if it gets used widely.

For the C++ standards issue I have implemented other work-arounds.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right now I don't know if we should merge it.
It adds complexity, but might lead to problems if it gets used widely.

I share your concern.

For the C++ standards issue I have implemented other work-arounds.

Depending on which CMake versions you need to support CMAKE_CXX_STANDARD might be an interesting option too (to be used in your enable_cxx11_support package).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Depending on which CMake versions you need to support CMAKE_CXX_STANDARD might be an interesting option too (to be used in your enable_cxx11_support package).

The package was meant for indigo as well, so I don't want to rely on new CMake features.
It seems like most users have settled to set the C++11 options in any depending package :-/

break()
elseif(elem MATCHES ${expr})
set(found TRUE)
message(${ARG_LEVEL} "could not add '${item}' (from '${ARG_CONTEXT}'), already contained: '${elem}'")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if this is intentionally passed? Related to the question above.

Comment thread .travis.yml Outdated
@@ -1,4 +1,6 @@
language: python
dist: trusty
sudo: required

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this necessary for this PR? If not please remove it from this patch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure about sudo: required (I guess this is the default anyways.)
dist: trusty is required because of cmake_minimum_required(VERSION 2.8.12), which is needed for add_compile_options in the tests. As an alternative add_definitions could be used though.

Comment thread cmake/all.cmake
list_append_deduplicate
list_append_unique
list_insert_in_workspace_order
list_insert_options

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would list_append_unique_options make more sense for the name? Since the values are always appended. The only difference to the list_append_unique function above is that is only considers the key part and not the value when checking for existence.

@mathias-luedtke mathias-luedtke Jul 17, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not in favor of a particular name.
I have chosen "insert" because of the setmap logic, but if you like list_append_unique_options better that's fine for me as well.

@ros ros deleted a comment from nuclearsandwich Jul 17, 2017
# :type ARGN: list key[=value] strings
#
# :param CONTEXT: context to be printen in case of erros
# :param CONTEXT: context to be printen in case of errors

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

*printed

@mathias-luedtke

Copy link
Copy Markdown
Contributor Author

I can address the simple issues tomorrow.

However, I am not sure about the first/last-issue.
Looks like the values in catkin_package actually get preferred.

In which order do the dependencies get resolved?
The topological order at build time?

@dirk-thomas

Copy link
Copy Markdown
Member

In which order do the dependencies get resolved?
The topological order at build time?

I don't think the order will equal the topological order at build time. Especially since there might be packages which don't have to processed sequentially but can be build in parallel.

From a CMake point of view it calls find_package of all COMPONENTS sequentially and each of these packages will again find_package its exported dependencies. So it is more like "depth first".

@mathias-luedtke

Copy link
Copy Markdown
Contributor Author

From a CMake point of view it calls find_package of all COMPONENTS sequentially and each of these packages will again find_package its exported dependencies. So it is more like "depth first".

"depth first" with the order of depends passed by the author of each package.
It is alphabetical by convention, but this is not enforced. I would consider it more or less arbitrary then.

@mathias-luedtke

Copy link
Copy Markdown
Contributor Author

As discussed this feature might introduce more problems (conflicting settings..) than it solves.
That's why I am closing this PR.

@dirk-thomas: Thanks for reviewing and discussing it!

@dirk-thomas

Copy link
Copy Markdown
Member

Thank you for your effort!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants