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

[multitop] Add better support for linker scripts (preprocessing) #25908

Merged
merged 6 commits into from
Jan 20, 2025

Conversation

pamaury
Copy link
Contributor

@pamaury pamaury commented Jan 16, 2025

Alternative approach to #25785 that preprocesses linker scripts instead of using bazel templates.

For now, this PR choses the "easy" way of keep each's top linker file name and set a defines so that users that include the correct file without knowing the name. An alternative approach would be to rename the file so it's the same for all tops. Note that even renaming would not render the preprocess useless because further multitop changes down the line cannot be handled by just renaming.

Annoyance: I couldn't make cc_common.compile to work because it does not add anything to the cout output so one can't get the output file... I had to resort to manually run the compiler, I don't know if there is an easier way to do that. It's probably semi-overkill because for the purpose of preprocessing linker script, it's very unlikely that the environment or system includes will play any role but at least it's the "textbook" way of doing it.

@pamaury pamaury changed the title [multitop] Add better support for linker scripts [multitop] Add better support for linker scripts (preprocessing) Jan 16, 2025
Copy link
Contributor

@jwnrt jwnrt left a comment

Choose a reason for hiding this comment

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

I've gone over the Bazel part a couple of times and it looks good. Nice to see the get_tool_for_action used. (aside, I realised that I missed this for the other part of our repo that uses the preprocessor here: rules/ujson.bzl#L40).

I think this is better than using Bazel substitution as discussed in the other PR.

LGTM, thanks

@pamaury pamaury force-pushed the multitop_linker_scripts2 branch 2 times, most recently from c24080a to 9683aeb Compare January 16, 2025 18:25
rules/linker.bzl Outdated Show resolved Hide resolved
rules/linker.bzl Outdated Show resolved Hide resolved
@pamaury pamaury force-pushed the multitop_linker_scripts2 branch from 9683aeb to 5ac9ac0 Compare January 16, 2025 20:33
@pamaury
Copy link
Contributor Author

pamaury commented Jan 16, 2025

One thing to note about this PR which may be confusing: it gives two ways to include files: INCLUDE and #include. The files which are #include-ed are obviously preprocessed but the files which are INCLUDE-ed are not. Maybe we should make that clear in the documentation (it's technically there, but might not be obvious).

@pamaury pamaury force-pushed the multitop_linker_scripts2 branch from 5ac9ac0 to d903a15 Compare January 16, 2025 21:10
Copy link
Contributor

@engdoreis engdoreis left a comment

Choose a reason for hiding this comment

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

I prefer this approach over the template.

hw/top/defs.bzl Outdated Show resolved Hide resolved
With this commit, linker script files are now preprocessed.
The is handled similarly to `cc_library`: if `ld_library` is used
with only files in `includes`, then it simply adds them to
the compilation context. If `ld_library` is used with `script` set,
then the file with be preprocessed with the compilation context
constructed from its dependencies.

This commit also adds a `defines` attribute to the `ld_library` so
that defines can be set in bazel BUILD files.

Signed-off-by: Amaury Pouly <[email protected]>
This avoid including by mistake the wrong file: because it is
marked as incompatible, it will fail to compile.

Signed-off-by: Amaury Pouly <[email protected]>
Each top has a different top linker file name. When dependencies
switch to multitop, then need to INCLUDE the correct file. This
commit adds a define OPENTITAN_TOP_MEMORY_LD which is set to
the file name so that user can do
INCLUDE OPENTITAN_TOP_MEMORY_LD

Signed-off-by: Amaury Pouly <[email protected]>
The OPENTITAN_TOP_MEMORY_LD define is set in each top's linker
library target.

Signed-off-by: Amaury Pouly <[email protected]>
@pamaury pamaury force-pushed the multitop_linker_scripts2 branch from d903a15 to 6fa9fc1 Compare January 17, 2025 15:10
"-E", # Preprocess only.
"-P", # Avoid line markers in output.
"-C", # Keep comments
"-xc", # Force C language.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean that C++ comments (// foo ) are not permitted?

Considering that is already the case for assembly language files, its probably not an issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes only C comments are supported. I think this is more consistent because linker scripts only allow /* */ for comments when they are not preprocessed.

Copy link
Contributor

@a-will a-will Jan 17, 2025

Choose a reason for hiding this comment

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

They should pass through the preprocessor with -C (or maybe it needs -CC... I forget!), so you don't have to maintain that restriction, I think. Doesn't matter to me, though!

@pamaury pamaury merged commit e2537bc into lowRISC:master Jan 20, 2025
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants