Skip to content

Commit

Permalink
Fix warning about #pragma once in main file
Browse files Browse the repository at this point in the history
We do some manual header insertion into the ROS2 bindings `.cc` file to fix some import errors. This commit skips copying of the `#pragma once` attribute at the beginning to avoid a C++ warning.
  • Loading branch information
phil-opp committed Feb 29, 2024
1 parent edb2a28 commit c92bad3
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions apis/c++/node/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ fn target_dir() -> PathBuf {
#[cfg(feature = "ros2-bridge")]
mod ros2 {
use super::target_dir;
use std::path::{Component, Path, PathBuf};
use std::{
io::{BufRead, BufReader},
path::{Component, Path, PathBuf},
};

pub fn generate() -> PathBuf {
use rust_format::Formatter;
Expand Down Expand Up @@ -118,15 +121,25 @@ mod ros2 {
std::fs::copy(&header_path, &target_path).unwrap();
println!("cargo:rerun-if-changed={}", header_path.display());

let mut node_header =
let node_header =
std::fs::File::open(target_path.with_file_name("dora-node-api.h")).unwrap();
let mut code_file = std::fs::File::open(&code_path).unwrap();
println!("cargo:rerun-if-changed={}", code_path.display());
let mut code_target_file =
std::fs::File::create(target_path.with_file_name("dora-ros2-bindings.cc")).unwrap();

// copy both the node header and the code file to prevent import errors
std::io::copy(&mut node_header, &mut code_target_file).unwrap();
let mut header_reader = {
let mut reader = BufReader::new(node_header);

// read first line to skip `#pragma once`, which is not allowed in main files
let mut first_line = String::new();
reader.read_line(&mut first_line).unwrap();
assert_eq!(first_line.trim(), "#pragma once");

reader
};
std::io::copy(&mut header_reader, &mut code_target_file).unwrap();
std::io::copy(&mut code_file, &mut code_target_file).unwrap();
code_target_file.flush().unwrap();
}
Expand Down

0 comments on commit c92bad3

Please sign in to comment.