LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-19-2022, 04:25 PM   #1
z3rOR0ne
LQ Newbie
 
Registered: Nov 2021
Distribution: Artix
Posts: 10

Rep: Reputation: Disabled
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.
 
Old 04-19-2022, 04:49 PM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,767

Rep: Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765
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).
 
Old 04-19-2022, 06:43 PM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by z3rOR0ne View Post
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.

It stands to be a lot of work.
 
Old 04-19-2022, 07:06 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Haven't looked recently, but let's settle on say 25 million loc.
Should occupy a few weekends.
 
Old 04-19-2022, 07:37 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
Quote:
Originally Posted by z3rOR0ne View Post
compiling your own C compiler and then using that compiler to compile your own Linux Kernel
Not difficult. I can do it with my eyes closed.
 
Old 04-21-2022, 12:43 AM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
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 View Post
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.

Last edited by ondoho; 04-21-2022 at 12:45 AM.
 
Old 04-21-2022, 10:52 AM   #7
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,767

Rep: Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765
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.
 
Old 04-21-2022, 01:57 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
Quote:
Originally Posted by ondoho View Post
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.
 
Old 04-21-2022, 06:55 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 04-29-2022, 09:00 PM   #10
DoctorWizard
Member
 
Registered: Feb 2019
Location: Orange Park, FL
Distribution: Mint 20.3
Posts: 43

Rep: Reputation: Disabled
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!
 
Old 04-29-2022, 11:59 PM   #11
Michael Uplawski
Senior Member
 
Registered: Dec 2015
Posts: 1,623
Blog Entries: 40

Rep: Reputation: Disabled
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.
 
Old 04-30-2022, 12:00 PM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,691
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
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.
 
Old 04-30-2022, 12:08 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by ondoho View Post
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
 
Old 04-30-2022, 12:21 PM   #14
EdGr
Senior Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 1,003

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
A small compiler is practical to write.

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
 
Old 04-30-2022, 02:30 PM   #15
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Quote:
Originally Posted by EdGr View Post
A small compiler is practical to write.

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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Create Your Own Custom Light and Dark Dynamic Wallpaper for GNOME LXer Syndicated Linux News 0 04-09-2022 03:39 AM
suddenly vlc audio chunky. got sound then no sound then got sound then no sound.. andrewysk Linux - Newbie 4 04-21-2021 08:19 PM
DISCUSSION: Create your own custom Live Linux CD mchirico LinuxAnswers Discussion 3 05-16-2019 05:48 PM
Do You Compile Your Own Kernel or Use The One Shipped With Your Distribution? jeremy Linux - General 132 11-17-2016 11:11 AM
LXer: 6 Tools to Easily Create Your Own Custom Linux Distro LXer Syndicated Linux News 0 04-09-2010 12:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration