Skip to content
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Rust build and example test

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose

- name: "C/C++ toolchain install"
run: |
sudo apt update && sudo apt install build-essential tree

- name: "example build test"
run: |
cargo run ./example/hello_world
tree ./example/hello_world

7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
/build/

# example production files
/example/hello_world/bin/
/example/hello_world/build/
/example/**/bin/
/example/**/build/
/example/**/output/
/example/**/*.o
/example/**/*.obj
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cxon"
authors = ["Cora"]
version = "0.1.2"
version = "0.1.3"
edition = "2024"
readme = "README.md"
repository = "https://github.com/CoraBlack/cxon"
Expand Down
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@
>
> - cxon is still in development, and the configrantion field is not stable yet.

## Installation

- ### Build and insall from cargo
```sh
cargo install cxon
```

- ### [Install directly from github release](https://github.com/CoraBlack/cxon/releases)

## Features(Order by priority)

- [x] Build project with compiler and linker by cxon.json immediately.(unsupport custom)
- [x] Build cache
- [x] Mult-thread build.
- [x] Muti-build-target.
- [ ] cxon.json schema support
- [ ] Debug field support(Only debug currently).
- [ ] Submodule support.
- [ ] Multiple compile targets.
- [ ] Platform-specific configuration.
- [ ] Build for cmake.

## What is the goal for cxon?

Expand All @@ -24,7 +35,7 @@ We only require a small number of essential fields for cxon.json and make the bu
{
"project": "HelloWorld", // (Required) project name
"target_name": "hello", // the final compiled product name, the default value is the project field
"target_type": "execuable", // (Required) build type (execuable(only currently), static_lib, shared_lib, object_lib)
"target_type": "execuable", // (Required) build type (execuable, static_lib, shared_lib, object_lib)
"build_dir": "build", // the directory storing intermediate compiled product
"output_dir": "bin", // the directory storing final compiled product

Expand Down
22 changes: 0 additions & 22 deletions example/hello_world/cxon.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@
"target_type": "executable",
"build_dir": "build",
"output_dir": "bin",

"toolchain": "gnu",

"flags": [

],

"cflags": [

],

"cxxflags": [
"-std=c++11"
],
Expand All @@ -22,20 +12,8 @@
"./include/"
],

"defines": [

],

"sources": [
"./main.cpp",
"./func.cpp"
],

"link": [

],

"libs": [

]
}
9 changes: 9 additions & 0 deletions example/shared_lib/cxon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"project": "my_shared_lib",
"toolchain": "llvm",
"target_type": "shared_lib",

"sources": [
"./func.cpp"
]
}
6 changes: 6 additions & 0 deletions example/shared_lib/func.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "./include/func.hpp"

void add(int a, int b) {
std::cout << "The sum is: " << a + b << std::endl;
}
1 change: 1 addition & 0 deletions example/shared_lib/include/func.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void add(int a, int b);
9 changes: 9 additions & 0 deletions example/static_lib/cxon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"project": "my_static_lib",
"toolchain": "llvm",
"target_type": "static_lib",

"sources": [
"./func.cpp"
]
}
6 changes: 6 additions & 0 deletions example/static_lib/func.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <iostream>
#include "./include/func.hpp"

void add(int a, int b) {
std::cout << "The sum is: " << a + b << std::endl;
}
1 change: 1 addition & 0 deletions example/static_lib/include/func.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void add(int a, int b);
2 changes: 1 addition & 1 deletion src/cli/arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl CliArgs {

let project_dir = PathBuf::from(arg_col[1].clone());
if !project_dir.exists() {
Err(()).expect("cxon project dir is not available")
Err(()).expect(format!("cxon project dir is not available: {}", project_dir.display()).as_str())
}

// remove cxon.json if it's included in the path
Expand Down
4 changes: 2 additions & 2 deletions src/cxon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,11 @@ fn default_target_type() -> String {
}

fn default_build_dir() -> PathBuf {
PathBuf::from("./build").canonicalize().expect("Failed to canonicalize build directory")
PathBuf::from("./build")
}

fn default_output_dir() -> PathBuf {
PathBuf::from("./output").canonicalize().expect("Failed to canonicalize output directory")
PathBuf::from("./output")
}

#[test]
Expand Down
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{collections::VecDeque, sync::{Arc, Mutex}, thread};

use crate::{cxon::get_cxon_config, object::{output::ObjectCollection, source::Source}, toolchain::{ToolChain, ToolChainTrait, compiler, gnu::GNU, llvm::LLVM, msvc::MSVC}};
use crate::{cxon::get_cxon_config, object::{output::ObjectCollection, source::Source}, toolchain::{ToolChain, ToolChainTrait, compiler, gnu::GNU, linker, llvm::LLVM, msvc::MSVC}};

pub mod cli {
pub mod arg;
Expand All @@ -14,7 +14,11 @@ pub mod utils;
pub mod cxon;

fn main() -> () {
let toolchain = get_cxon_config().read().unwrap().get_toolchain();
let toolchain = get_cxon_config()
.read()
.unwrap()
.get_toolchain();

match toolchain {
ToolChain::GNU() => build_project::<GNU>(),
ToolChain::LLVM() => build_project::<LLVM>(),
Expand Down Expand Up @@ -63,5 +67,10 @@ fn build_project<T: ToolChainTrait>() {
thread.join().unwrap();
}

toolchain::linker::link_to_executable::<T>(objects.lock().unwrap().clone());
linker::link::<T>(objects.lock().unwrap().clone(),
get_cxon_config()
.read()
.unwrap()
.get_target_type()
);
}
4 changes: 2 additions & 2 deletions src/toolchain/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct CompileFuncArgs {
}

pub fn compile<T: ToolChainTrait>(src: Source) -> Object {
let obj_path = get_object_target_path(&src).expect("Failed to get the target path of object file");
let obj_path = get_object_target_path::<T>(&src).expect("Failed to get the target path of object file");

if !need_recompile(&src, &obj_path) {
return Object {
Expand Down Expand Up @@ -56,7 +56,7 @@ fn compile_handler<T: ToolChainTrait>(args: CompileFuncArgs) -> Object {
.arg(T::DEBUG_FLAG)
.arg(T::ONLY_COMPILE_FLAG)
.arg(args.src_path.to_str().unwrap())
.arg(T::OUTPUT_FLAG)
.arg(T::EXECUTABLE_OUTPUT_FLAG)
.arg(args.obj_path.to_str().unwrap())
.args(args.includes)
.args(args.defines)
Expand Down
18 changes: 16 additions & 2 deletions src/toolchain/gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ pub struct GNU{
impl ToolChainTrait for GNU {
const CC: &'static str = "gcc";
const CXX: &'static str = "g++";
const LINKER: &'static str = "g++";
const DEBUG_FLAG: &'static str = "-g";
const OUTPUT_FLAG: &'static str = "-o";

const EXECUTABLE_LINKER: &'static str = "g++";
const STATIC_LIB_LINKER: &'static str = "ar";
const SHARED_LIB_LINKER: &'static str = "g++";
const OBJECT_LIB_LINKER: &'static str = "lr";

const EXECUTABLE_OUTPUT_FLAG: &'static str = "-o";
const STATIC_LIB_OUTPUT_FLAG: &'static str = "rcs";
const SHARED_LIB_OUTPUT_FLAG: &'static str = "-shared -fPIC -o";
const OBJECT_LIB_OUTPUT_FLAG: &'static str = "-o";

const EXECUTABLE_EXTENSION: &'static str = "";
const STATIC_LIB_EXTENSION: &'static str = "a";
const SHARED_LIB_EXTENSION: &'static str = "so";
const OBJECT_LIB_EXTENSION: &'static str = "o";

const ONLY_COMPILE_FLAG: &'static str = "-c";
const DEFINE_FLAG_PREFIX: &'static str = "-D";
const INCLUDE_FLAG_PREFIX: &'static str = "-I";
Expand Down
Loading