cargo-gccrs - GSoC Progress report

2021-07-02 Thread cohenarthur.dev via Gcc
# Overview

The goal of `cargo-gccrs` is to allow rust's building system, cargo [1]
to use `gccrs` as an alternative compiler to `rustc`.

In order to allow the rust language to target more of the current architecture 
ecosystem,
as well as help in resolving the bootstrapping problem, rust support for the 
GNU Compiler
Collection is currently being developed. The compiler is getting more and more 
usable
for rust code in general, and could definitely benefit from more usage from the 
community.
Adding support for `gccrs` to the `cargo` build system would be a stepping 
stone in having
more of rust users test the compiler and use it.

# Progress Report

## Achieved functionality

Work on `cargo-gccrs` started at the beginning of the month of June. In that 
time, we have
managed to allow the compilation of simple projects, in order to generate 
correct binaries
in the formats of an executable, a shared library or a static library.

We have designed an option parser that is able to translate `rustc` options 
into valid
`gccrs` options. The program then spawns a `gccrs` command with valid arguments 
in order
to compile the given rust project.

We added a few basic projects to the repository, that are compilable using 
`gccrs`
(this means only one file, no rust macros, etc). We also worked on 
incorporating a testing
framework to make sure that `cargo-gccrs` can compile those basic projects as 
it is being
tweaked and worked on.

If your project is simple enough, you are now able to use `cargo-gccrs` for 
building and
running it. Simply use `cargo gccrs build` or `cargo gccrs run`.

## Problems encountered

`gcc` cannot produce multiple binaries with one command:

The rust compiler is often invoked by cargo with the goal of generating a 
binary, a static
library and a shared library. This is achieved by passing multiple instances of 
the `--crate-type`
argument, which `gcc` does not have an equivalent for. Therefore, `cargo-gccrs` 
might have
to resort to spawning multiple `gccrs` commands in order to reproduce the 
behavior of `rustc`.

`gcc` cannot directly produce static libraries:

`rustc` can directly create archive files containing multiple object files. On 
Unix, this
corresponds to the `.a` file format, and these archives are created using the 
`ar`
command. `gcc` does not have any means to do that, and `cargo-gccrs` has to 
spawn a secondary
command using the `ar` program, whose aim is to package a previously created 
compilation unit
generated using `gccrs`.

`gccrs` cannot generate the expected filenames of binaries:

When displaying the compiler's configuration, `rustc` can print the expected 
output filenames
of different binaries. For example, on Unix systems, a shared library shall be 
called
`lib.so`. A binary can simply be called ``, and a static library 
will usually
be called `lib.a`. This being displayed is one of the requirements for 
`cargo` to
work with the compiler. As this isn't implemented yet, an issue has been opened
here [2] to discuss it.

This work was achieved thanks to the thoughtful reviews and contributions from
@philberty (Philip Herron) [3], @flip1995 [4] and @bjorn3 [5]. Thanks a lot for 
your implication!

# Goals for this GSoC

The goals for this GSoC were as follow:

- Create a barebones rust project as a cargo subcommand
- Get familar with `gccrs` internals
- Build initial `cargo-gccrs` with basic functionality
- Investigate `cargo` in depth in order to replicate `rustc`'s behavior as 
closely
as possible
- Allow for users to compile all sorts of binaries
- Add abstractions around testing in order for the project to benefit from the 
large
amount of rust code already written
- Report any kind of issue with the compiler to the main `gccrs` repository
- If time is left, work on fixing those issues directly in the compiler.

The project is progressing nicely. We are on track to having most of the 
functionality
implemented, cleaned-up and well tested. A lot of work still remains, and a lot 
of issues
are yet to be discovered. I should have some time to dedicate to contributing 
directly to
`gccrs` in order for `cargo-gccrs` to be even more useful. I think that I am on 
track to
complete a minimum viable product for this GSoC project, which could then be 
easily
maintained and improved upon as the compiler progresses.

# Status Table

|Category| Last Month | This Month | Delta|
||||--|
|Todo|   0|   17   | +17  |
|In Progress |   0|   3| +3   |
|Completed   |   0|   6| +6   |
|Blocked |   0|   0| 0|

# Risks

The `gccrs` compiler does not implement all of the functionality that the 
`rustc` compiler
provides. For example, a lot of rust projects use multiple files, which are not 
handled
yet. This reduces the usability of the tool as it is not yet able to compile 
multiple
projects. In the same vein, macros and procedural macros are not supporte

C-family selftests in language-independant source files

2021-11-05 Thread cohenarthur.dev via Gcc
Hi everyone,

We have been trying to enable the use of selftests for the rust frontend
over at gccrs. While doing this, I have realized that a few tests from
language-independant source files such as `opt-problem.c` and
`diagnostic.c` actually rely on the compiler being a C one.

For example, one test asserts multiple time that a dumped text actually
contains the "int" keyword for type assertions, which is never present
in gccrs's error messages.

In order to enable the selftests, I have added the following line to our
rust/Make-lang.in, amongs others:

RUST_SELFTEST_FLAGS = -xr $(SELFTEST_FLAGS)

Passing -xc instead enables the opt-problem and diagnostic tests to
pass, but causes our tests to not run. Passing -xrs causes our tests to
run, but the opt-problem and diagnostic selftests to fail.

Any idea as to how to disable those tests? Or make it so that they are
only ran when running C/C++ selftests?

Thanks a lot!

Kindly,

-- 
Arthur Cohen


Re: C-family selftests in language-independant source files

2021-11-05 Thread cohenarthur.dev via Gcc
Hi Dave,

On 21/11/05 09:23AM, David Malcolm wrote:
> On Fri, 2021-11-05 at 10:38 +0100, cohenarthur.dev via Gcc wrote:
> > Hi everyone,
> > 
> > We have been trying to enable the use of selftests for the rust
> > frontend
> > over at gccrs. While doing this, I have realized that a few tests from
> > language-independant source files such as `opt-problem.c` and
> > `diagnostic.c` actually rely on the compiler being a C one.
> > 
> > For example, one test asserts multiple time that a dumped text actually
> > contains the "int" keyword for type assertions, which is never present
> > in gccrs's error messages.
> > 
> > In order to enable the selftests, I have added the following line to
> > our
> > rust/Make-lang.in, amongs others:
> > 
> > RUST_SELFTEST_FLAGS = -xr $(SELFTEST_FLAGS)
> > 
> > Passing -xc instead enables the opt-problem and diagnostic tests to
> > pass, but causes our tests to not run. Passing -xrs causes our tests to
> > run, but the opt-problem and diagnostic selftests to fail.
> > 
> > Any idea as to how to disable those tests? Or make it so that they are
> > only ran when running C/C++ selftests?
> 
> If a selftest should only be run for a given language, there's a
> langhook called by selftest::run_tests:
> 
>   /* Run any lang-specific selftests.  */
>   lang_hooks.run_lang_selftests ();
> 
> which e.g. the C frontend implements in gcc/c/c-lang.c as:
> 
> #if CHECKING_P
> #undef LANG_HOOKS_RUN_LANG_SELFTESTS
> #define LANG_HOOKS_RUN_LANG_SELFTESTS selftest::run_c_tests
> #endif /* #if CHECKING_P */
> 
> which currently merely calls
>   c_family_tests ();
> which is defined in gcc/c-family/c-common.c
> 
> So the invocation of c-family-specific tests could be moved to there
> (or to the appropriate locations for C/C++ tests), splitting things up
> as appropriate based on how much of each file's selftest suite is lang-
> specific.

That's a very nice idea, thanks a lot. I did not think of it.

> If it's just one assert within a larger selftest that's problematic,
> maybe we can conditionalize it individually, though I'm not sure of a
> good way to do that off the top of my head.

It sadly is one assert through a big test, but I believe that the whole
test is not really language specific: It tries to parse a `test.c` file,
tries to assert some loop optimization failures, and so on.

I personally think it should be moved to C and C++ specific tests. I'm
assuming it was put those files so that both frontends would try and
pass them, as no other frontend uses the selftest framework.

Thanks a lot for the answer, this is very helpful