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

pref: cleanup Backend and CompilerType functions, make minor performance adjustments #21389

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 23 additions & 36 deletions vlib/v/pref/pref.v
Original file line number Diff line number Diff line change
Expand Up @@ -846,13 +846,12 @@ pub fn parse_args_and_show_errors(known_external_commands []string, args []strin
b := backend_from_string(sbackend) or {
eprintln_exit('Unknown V backend: ${sbackend}\nValid -backend choices are: c, go, interpret, js, js_node, js_browser, js_freestanding, native, wasm')
}
if b.is_js() {
res.output_cross_c = true
}
if b == .wasm {
Copy link
Contributor

Choose a reason for hiding this comment

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

Need a b.is_wasm() to align with the other backends.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay checking, it's correct as it is. For the backends there only is .wasm. For the oses there is:

		.wasm32,
		.wasm32_emscripten,
		.wasm32_wasi,
		// Native wasm options:
		.wasi,
		.browser,

Copy link
Contributor

Choose a reason for hiding this comment

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

There are several js output variations as well...

Note that `js` defaults to the `node` codegen backend but it's also possible to
pick another:

* `js_browser`        - V outputs JS source code ready for the browser.
* `js_node`           - V outputs JS source code to run with nodejs.
* `js_freestanding`   - V outputs JS source code with no hard runtime dependency.

wasm is definitely set up different from the others, if it differentiates via os rather than backend.

res.compile_defines << 'wasm'
res.compile_defines_all << 'wasm'
res.arch = .wasm32
} else if b.is_js() {
res.output_cross_c = true
}
res.backend = b
i++
Expand Down Expand Up @@ -1114,46 +1113,34 @@ fn is_source_file(path string) bool {
pub fn backend_from_string(s string) !Backend {
// TODO: unify the "different js backend" options into a single `-b js`
// + a separate option, to choose the wanted JS output.
match s {
'c' { return .c }
'go' { return .golang }
'interpret' { return .interpret }
'js' { return .js_node }
'js_node' { return .js_node }
'js_browser' { return .js_browser }
'js_freestanding' { return .js_freestanding }
'native' { return .native }
'wasm' { return .wasm }
else { return error('Unknown backend type ${s}') }
return match s {
'c' { .c }
'interpret' { .interpret }
'js', 'js_node' { .js_node }
'js_browser' { .js_browser }
'js_freestanding' { .js_freestanding }
'wasm' { .wasm }
'native' { .native }
'go' { .golang }
else { error('Unknown backend type ${s}') }
}
}

// Helper function to convert string names to CC enum
pub fn cc_from_string(cc_str string) CompilerType {
if cc_str.len == 0 {
pub fn cc_from_string(s string) CompilerType {
if s == '' {
return .gcc
}
// TODO
normalized_cc := cc_str.replace('\\', '/')
normalized_cc_array := normalized_cc.split('/')
last_elem := normalized_cc_array.last()
cc := last_elem.all_before('.')
if cc.contains('++') {
return .cplusplus
}
if cc.contains('tcc') || cc.contains('tinyc') {
return .tinyc
}
if cc.contains('clang') {
return .clang
}
if cc.contains('mingw') {
return .mingw
}
if cc.contains('msvc') {
return .msvc
cc := os.file_name(s).to_lower()
return match true {
cc.contains('tcc') || cc.contains('tinyc') { .tinyc }
cc.contains('gcc') { .gcc }
cc.contains('clang') { .clang }
cc.contains('msvc') { .msvc }
cc.contains('mingw') { .mingw }
cc.contains('++') { .cplusplus }
else { .gcc }
}
return .gcc
}

fn (mut prefs Preferences) parse_define(define string) {
Expand Down