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

Implement host_flag? macro method, not affected by cross-compilation #9049

Merged
merged 4 commits into from
May 11, 2020

Conversation

oprypin
Copy link
Member

@oprypin oprypin commented Apr 12, 2020

The flag? macro tells you information about the target architecture, but one may need to run a command at compile time (so on the host), and it would be a different command depending on which OS it is.

Example:

diff --git a/src/compiler/crystal/config.cr b/src/compiler/crystal/config.cr
index 8b756583e..38215b7c8 100644
--- a/src/compiler/crystal/config.cr
+++ b/src/compiler/crystal/config.cr
@@ -32,7 +32,9 @@ module Crystal
     end
 
     def self.date
-      time = {{ (env("SOURCE_DATE_EPOCH") || `date +%s`).to_i }}
+      time = {{ (env("SOURCE_DATE_EPOCH") || (
+                  host_flag?(:win32) ? `powershell Get-Date -Millisecond 0 -UFormat %s` : `date +%s`
+                )).to_i }}
       Time.unix(time).to_s("%Y-%m-%d")
     end

The `flag?` macro tells you information about the target architecture, but one may need to run a different command at compile time (so *on the host*), and it would be a different command depending on which OS it is.
@oprypin oprypin changed the title Implement host_flag? macro, not affected by cross-compilation Implement host_flag? macro method, not affected by cross-compilation Apr 12, 2020
@asterite
Copy link
Member

asterite commented Apr 12, 2020

This makes me wonder if we should be computing all of those things with macros.

That said, this needs to also be added to the macro docs (search for def flag? and you'll find it).

@oprypin
Copy link
Member Author

oprypin commented Apr 13, 2020

The bad thing here isn't really the macros but the shelling out :)
Each instance of that is technical debt, and we'll be scratching our heads now that Windows is relevant.
Definitely the most fun instance of this is ldflags: "`stuff`"

@[Link(ldflags: "`{{LibLLVM::LLVM_CONFIG.id}} --libs --system-libs --ldflags --link-static 2> /dev/null`")]
See, no macros there :)

@oprypin
Copy link
Member Author

oprypin commented Apr 13, 2020

needs to also be added to the macro docs

I realized it later, but that also made me think, maybe we should keep it undocumented for now. I don't know if we'll want to keep it, in this shape, or at all.

First off, I'm thinking to resolve that particular example above with macro run instead.

Then I also noticed that some important use cases are missing with this data representation. E.g. what if I want to check whether host OS differs from target OS? Should I go through every possible pair of flags and check if they're not equal?

So yeah, I'm mostly hedging my bets at this point, because this will need a Crystal release before it can be used.

@waj
Copy link
Member

waj commented May 7, 2020

@oprypin could you please fix the conflict so we can merge this? Thanks!

@waj waj added this to the 0.35.0 milestone May 7, 2020
Copy link
Member

@straight-shoota straight-shoota 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 looked at the previous discussion in #7196 which also touched this. Seems this is on par with what we considered back then 👍
Still have two suggestions to improve this PR (but it's not a necessity).

src/compiler/crystal/macros/methods.cr Show resolved Hide resolved
@@ -11,6 +12,10 @@ class Crystal::Program
@flags ||= flags_for_target(codegen_target)
end

def host_flags
@host_flags ||= flags_for_target(Config.default_target)
Copy link
Member

Choose a reason for hiding this comment

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

I'd also suggest to rename Config.default_target to Config.host_target because that's what it is.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, didn't turn out too big.

src/llvm.cr Outdated Show resolved Hide resolved
@oprypin
Copy link
Member Author

oprypin commented May 8, 2020

Woops, pushed a bad commit previously, should be good now

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.

6 participants