How difficult is it to create your own C compiler and then use that compiler to compile your own custom Linux kernel?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How difficult is it to create your own C compiler and then use that compiler to compile your own custom Linux kernel?
Hi all, first post here.
I'm not exactly a newbie, but have only used Linux daily for almost 1.5 years now and not at the fastest paste. I started off with Manjaro for a year, then for the last 6 months or so have been using Artix Linux. I also very recently started using my first twm, i3 (previously on xfce4 where I found myself creating custom keybindings so similar to i3 that I switched).
Anywho, introductions aside, I was recently looking into whether installing Gentoo was really all that difficult, and I generally got the impression that yeah, you follow the guides, and it more or less is easy, someone had suggested that compiling your own C compiler and then using that compiler to compile your own Linux Kernel in a Linux From Scratch scenario was a good test of whether you had good "Linux-Fu."
Has anyone tried this? I have only played around with bash, python, javascript, and a minimal amount of C programming at this point, and I have no idea where to start with this, any insight would be of great interest to me.
Thank you for taking the time to read. Thank you more if you post an insightful response.
That is a really VERY long way around to do the job! That said, obviously it can be done because some people have taken a few months out to DO it.
Step one is to get a railroad diagram of the language to include all of the features needed to compile the compiler itself.
Step two is to write the c source to create that compiler. Step three is to learn enough assembler to code those functions and create a compiler at the lowest level that can compile the Compiler source you have created.
An alternate to step three is to learn how to cross-compile and actually compile the compiler on a different platform and port it over to your target hardware.
Follow on steps are to improve the compiler AT THE C SOURCE LEVEL to add the additional features needed for a kernel build.
It gets tricky from there, but basically you need to design and build a kernel build environment. The exact features and methods really depend upon the nature of your target, your way of working, and your knowledge of development. The tricky thing is making the decisions and fine tuning your environment until you can get a successful build.
Personally, I would take an easier path. Many of us have taken the path of Learning at least one VERY low level programming, and C (or something like it) for working at the higher level, and then learning compiler construction, before we ever dove into kernel sources (or even WANTED to).
someone had suggested that compiling your own C compiler and then using that compiler to compile your own Linux Kernel in a Linux From Scratch scenario was a good test of whether you had good "Linux-Fu."
If you want to write a compiler, then write a compiler. If you want to write a kernel next, then write a kernel.
Given your later description that you've only played around a bit with some languages and have minimal C experience, I recommend you study the C language well enough to be able to write your own compiler for it. Or instead study the C language well enough to be able to write your own Linux kernel. In other words, tackle one large chunk at a time.
There are plenty of compiler design descriptions available via web searching.
The Linux kernel source is openly available, so you can read it to get some ideas how to construct your own kernel.
I don't see OP suggesting they want to write their own compiler, just compile it.
Obviously somebody somewhere must have compiled gcc, so the thought is reasonable.
Anywhoo, that someone that suggested "compiling your own C compiler and then using that compiler to compile your own Linux Kernel in a Linux From Scratch scenario was a good test of whether you had good "Linux-Fu" was either joking or - well, I don't know the context, but that seems vastly exaggerated to the point of suggesting that cat is the only real editor.
Quote:
Originally Posted by z3rOR0ne
I'm not exactly a newbie, but have only used Linux daily for almost 1.5 years now and not at the fastest paste. I started off with Manjaro for a year, then for the last 6 months or so have been using Artix Linux. I also very recently started using my first twm, i3 (previously on xfce4 where I found myself creating custom keybindings so similar to i3 that I switched).
Anywho, introductions aside, I was recently looking into whether installing Gentoo was really all that difficult, and I generally got the impression that yeah, you follow the guides, and it more or less is easy, someone had suggested that compiling your own C compiler and then using that compiler to compile your own Linux Kernel in a Linux From Scratch scenario was a good test of whether you had good "Linux-Fu."
Has anyone tried this? I have only played around with bash, python, javascript, and a minimal amount of C programming at this point, and I have no idea where to start with this, any insight would be of great interest to me.
One might locate a copy of LFS (Linux From Scratch) and pursue things from there.
One might disagree with the definition of "scratch" for some purposes, but it is nothing if not educational.
I don't see OP suggesting they want to write their own compiler, just compile it.
Fair enough point.
It's very common (or used to be) to reconfigure gcc and compile it for a different architecture. Yes you can just recompile gcc if you like also without reconfiguring it.
That part doesn't necessarily teach you Linux, but you'll learn about the tools the more you vary things and attempt changes or set up for cross compilation.
Building LFS is fairly guided in their documentation. You'll obviously learn more as you customize more.
Quick answer is "minutes", probably < 60 or 30 to just recompile gcc and build a LFS kernel. The rest is of any length depending how exhaustive you wish to try and customize.
If you are using Gentoo, as with "LFS = Linux From Scratch," then one of the very first steps that you do is to "bootstrap" the gcc compiler environment and the glibc library so that both of them are precisely aligned to your chosen target hardware environment, including CPU-type and so on. The procedures for doing so, while they do take a long time to "grind through," are carefully described.
Talk about reinventing the wheel! Way over my head but I have to give you credit for trying. Will be a great learning experience even if you fail catastrophically. Good luck!
I used to compile gcc (or g++ or both), when I was more into such stuff. It is not magic but can mean anything from a routine job to utter frustration. Be willing to narrow your eventual problem reports to the really verified facts, invest time into lecture and be honest with yourself.
ANTLR anyone? I used it in a so-called “serious project” and got pretty far before someone in charge (intelligent and compassionate at the same time) “decided” (understood) that continuing the use of a compiler-compiler might be a waste of time and effort.
My final conclusion is: [print-red]Do it for fun and stop immediately when it stops to be fun.[/print-red]
Last edited by Michael Uplawski; 04-30-2022 at 12:01 AM.
Although the role of "gcc" and "glibc" is a bit unusual in that "you must first compile them in order to compile them," the process is actually straightforward. It just takes a while.
I don't see OP suggesting they want to write their own compiler, just compile it.
This is the original title:
Quote:
How difficult is it to create your own C compiler and then use that compiler
I guess it is extremely hard. I mean to create a compiler which can produce acceptable result. And to create something which is better than the existing ones is almost impossible [for a single person].
But anyway, we have tools to generate compilers just we need to define the language properly.
(bison, yacc, flex...) https://www.linuxjournal.com/article/2227
I am looking at a compiler that I wrote for a language that I designed in the 1980s. The compiler is written in itself, compiles itself, and generates C. It is only 7K lines of code.
I am looking at a compiler that I wrote for a language that I designed in the 1980s. The compiler is written in itself, compiles itself, and generates C. It is only 7K lines of code.
The hard part is getting the first one.
Ed
I think this is the gist of the original question and the discussion here.
Writing a compiler is not all that difficult to start, and improving it is a recursive process. Improvement requires dedication, analysis, and often additional viewpoints to stimulate thought.
Write the code, compile it,
improve the code, recompile,
rinse and repeat. ad infinitum.
This is how all the compilers in use today began and got to where they are.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.