|
| 1 | +// Copyright 2016 Mozilla Foundation |
| 2 | +// SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +#![allow(unused_imports, dead_code, unused_variables)] |
| 17 | + |
| 18 | +use crate::compiler::args::*; |
| 19 | +use crate::compiler::c::{ArtifactDescriptor, CCompilerImpl, CCompilerKind, ParsedArguments}; |
| 20 | +use crate::compiler::cicc; |
| 21 | +use crate::compiler::{ |
| 22 | + CCompileCommand, Cacheable, ColorMode, CompileCommand, CompilerArguments, Language, |
| 23 | + SingleCompileCommand, |
| 24 | +}; |
| 25 | +use crate::{counted_array, dist}; |
| 26 | + |
| 27 | +use crate::mock_command::{CommandCreator, CommandCreatorSync, RunCommand}; |
| 28 | + |
| 29 | +use async_trait::async_trait; |
| 30 | + |
| 31 | +use std::collections::HashMap; |
| 32 | +use std::ffi::OsString; |
| 33 | +use std::fs; |
| 34 | +use std::path::{Path, PathBuf}; |
| 35 | +use std::process; |
| 36 | + |
| 37 | +use crate::errors::*; |
| 38 | + |
| 39 | +/// A unit struct on which to implement `CCompilerImpl`. |
| 40 | +#[derive(Clone, Debug)] |
| 41 | +pub struct CudaFE { |
| 42 | + pub version: Option<String>, |
| 43 | +} |
| 44 | + |
| 45 | +#[async_trait] |
| 46 | +impl CCompilerImpl for CudaFE { |
| 47 | + fn kind(&self) -> CCompilerKind { |
| 48 | + CCompilerKind::CudaFE |
| 49 | + } |
| 50 | + fn plusplus(&self) -> bool { |
| 51 | + true |
| 52 | + } |
| 53 | + fn version(&self) -> Option<String> { |
| 54 | + self.version.clone() |
| 55 | + } |
| 56 | + fn parse_arguments( |
| 57 | + &self, |
| 58 | + arguments: &[OsString], |
| 59 | + cwd: &Path, |
| 60 | + _env_vars: &[(OsString, OsString)], |
| 61 | + ) -> CompilerArguments<ParsedArguments> { |
| 62 | + cicc::parse_arguments(arguments, cwd, Language::CudaFE, &ARGS[..], 1) |
| 63 | + } |
| 64 | + #[allow(clippy::too_many_arguments)] |
| 65 | + async fn preprocess<T>( |
| 66 | + &self, |
| 67 | + _creator: &T, |
| 68 | + _executable: &Path, |
| 69 | + parsed_args: &ParsedArguments, |
| 70 | + cwd: &Path, |
| 71 | + _env_vars: &[(OsString, OsString)], |
| 72 | + _may_dist: bool, |
| 73 | + _rewrite_includes_only: bool, |
| 74 | + _preprocessor_cache_mode: bool, |
| 75 | + ) -> Result<process::Output> |
| 76 | + where |
| 77 | + T: CommandCreatorSync, |
| 78 | + { |
| 79 | + cicc::preprocess(cwd, parsed_args).await |
| 80 | + } |
| 81 | + fn generate_compile_commands<T>( |
| 82 | + &self, |
| 83 | + path_transformer: &mut dist::PathTransformer, |
| 84 | + executable: &Path, |
| 85 | + parsed_args: &ParsedArguments, |
| 86 | + cwd: &Path, |
| 87 | + env_vars: &[(OsString, OsString)], |
| 88 | + _rewrite_includes_only: bool, |
| 89 | + ) -> Result<( |
| 90 | + Box<dyn CompileCommand<T>>, |
| 91 | + Option<dist::CompileCommand>, |
| 92 | + Cacheable, |
| 93 | + )> |
| 94 | + where |
| 95 | + T: CommandCreatorSync, |
| 96 | + { |
| 97 | + generate_compile_commands(path_transformer, executable, parsed_args, cwd, env_vars).map( |
| 98 | + |(command, dist_command, cacheable)| { |
| 99 | + (CCompileCommand::new(command), dist_command, cacheable) |
| 100 | + }, |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +pub fn generate_compile_commands( |
| 106 | + path_transformer: &mut dist::PathTransformer, |
| 107 | + executable: &Path, |
| 108 | + parsed_args: &ParsedArguments, |
| 109 | + cwd: &Path, |
| 110 | + env_vars: &[(OsString, OsString)], |
| 111 | +) -> Result<( |
| 112 | + SingleCompileCommand, |
| 113 | + Option<dist::CompileCommand>, |
| 114 | + Cacheable, |
| 115 | +)> { |
| 116 | + // Unused arguments |
| 117 | + #[cfg(not(feature = "dist-client"))] |
| 118 | + { |
| 119 | + let _ = path_transformer; |
| 120 | + } |
| 121 | + |
| 122 | + let lang_str = &parsed_args.language.as_str(); |
| 123 | + let out_file = match parsed_args.outputs.get("obj") { |
| 124 | + Some(obj) => &obj.path, |
| 125 | + None => return Err(anyhow!("Missing {:?} file output", lang_str)), |
| 126 | + }; |
| 127 | + |
| 128 | + let mut arguments: Vec<OsString> = vec![]; |
| 129 | + arguments.extend_from_slice(&parsed_args.common_args); |
| 130 | + arguments.extend_from_slice(&parsed_args.unhashed_args); |
| 131 | + arguments.extend(vec![ |
| 132 | + "--module_id_file_name".into(), |
| 133 | + out_file.into(), |
| 134 | + (&parsed_args.input).into(), |
| 135 | + ]); |
| 136 | + |
| 137 | + if log_enabled!(log::Level::Trace) { |
| 138 | + trace!( |
| 139 | + "[{}]: {} command: {:?}", |
| 140 | + out_file.file_name().unwrap().to_string_lossy(), |
| 141 | + executable.file_name().unwrap().to_string_lossy(), |
| 142 | + [ |
| 143 | + &[format!("cd {} &&", cwd.to_string_lossy()).to_string()], |
| 144 | + &[executable.to_str().unwrap_or_default().to_string()][..], |
| 145 | + &dist::osstrings_to_strings(&arguments).unwrap_or_default()[..] |
| 146 | + ] |
| 147 | + .concat() |
| 148 | + .join(" ") |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + let command = SingleCompileCommand { |
| 153 | + executable: executable.to_owned(), |
| 154 | + arguments, |
| 155 | + env_vars: env_vars.to_owned(), |
| 156 | + cwd: cwd.to_owned(), |
| 157 | + }; |
| 158 | + |
| 159 | + #[cfg(not(feature = "dist-client"))] |
| 160 | + let dist_command = None; |
| 161 | + #[cfg(feature = "dist-client")] |
| 162 | + let dist_command = (|| { |
| 163 | + let mut arguments: Vec<String> = vec![]; |
| 164 | + arguments.extend(dist::osstrings_to_strings(&parsed_args.common_args)?); |
| 165 | + arguments.extend(dist::osstrings_to_strings(&parsed_args.unhashed_args)?); |
| 166 | + arguments.extend(vec![ |
| 167 | + "--module_id_file_name".into(), |
| 168 | + path_transformer.as_dist(out_file)?, |
| 169 | + path_transformer.as_dist(&parsed_args.input)?, |
| 170 | + ]); |
| 171 | + Some(dist::CompileCommand { |
| 172 | + executable: path_transformer.as_dist(executable.canonicalize().unwrap().as_path())?, |
| 173 | + arguments, |
| 174 | + env_vars: dist::osstring_tuples_to_strings(env_vars)?, |
| 175 | + cwd: path_transformer.as_dist_abs(cwd)?, |
| 176 | + }) |
| 177 | + })(); |
| 178 | + |
| 179 | + Ok((command, dist_command, Cacheable::Yes)) |
| 180 | +} |
| 181 | + |
| 182 | +use cicc::ArgData::*; |
| 183 | + |
| 184 | +counted_array!(pub static ARGS: [ArgInfo<cicc::ArgData>; _] = [ |
| 185 | + take_arg!("--gen_c_file_name", PathBuf, Separated, UnhashedOutput), |
| 186 | + flag!("--gen_module_id_file", GenModuleIdFileFlag), |
| 187 | + take_arg!("--module_id_file_name", PathBuf, Separated, Output), |
| 188 | + take_arg!("--stub_file_name", OsString, Separated, UnhashedPassThrough), |
| 189 | +]); |
0 commit comments