Chapter 3: Setting Up the Build System and Code Formatting
Introduction
As we dive deeper into the Sodum project, it's crucial to set up a proper build system and code formatting tools. These might seem like extra steps now, but trust me, they'll save us a lot of headaches down the road!
Build Systems: Why Bother?
Build systems are essential to automate the process of generating build files in a project. You might not need them for a simple project, but as the scale of your project grows, it becomes quite cumbersome to manually build the codebase. That's where build tools like CMake come into play.
There are various build tools in the market, such as Ninja, but for C++ projects, CMake is a decent option to go with. I chose CMake for Sodum because of its wide adoption in the C++ community and its flexibility.
CMake: Our Chosen Build System
Let's look at a sample CMake file I've put together for our project:
I have attatched a sample cmake file for reference but at this stage you might wanna do some googling and get familiarised with it although it will be almost similar to the provided example
cmake_minimum_required(VERSION 3.10)
project(Sodum)
set(CMAKE_CXX_STANDARD 17)
# Add source files
file(GLOB SOURCES "src/*.cpp")
# Include directories
include_directories(include)
# Create executable
add_executable(sodum ${SOURCES})
# Format code before building
add_custom_target(
format
COMMAND clang-format -i ${SOURCES} ${HEADERS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
add_dependencies(sodum format)
Don't get scared seeing this snippet! Here's what it does:
Specifies the minimum CMake version required
Sets the name of our project
Specifies the minimum C++ version required to build the project
Tells CMake where to find our source and header files
Sets up our executable
Integrates clang-format for code formatting
At this stage, you might want to do some Googling and get familiarized with CMake. While our usage will be similar to this example, understanding the basics will help you if we need to make changes later.
Clang-Format: Keeping Our Code Tidy
clang-format is a tool that helps to format our codebases so that consistency is maintained in terms of spacing and formatting our files. It's a real lifesaver when working on a project, especially with multiple contributors.
In our CMake file, we've set it up to run automatically before building the executable. This ensures that our code is always formatted consistently.
To use clang-format, you'll need to install it on your system. Once installed, it will be invoked by our CMake script whenever we build the project.
Wrapping Up
You may want to dive deep and explore how you want to use these tools in your project. Feel free to experiment and adjust the configurations to suit your preferences.
Next Steps
Now that we have our build system and formatting tools in place, we're ready to start the actual development phase of the language. In the next chapter, we'll have a look at tokens - the building blocks of our language. Get ready to roll up your sleeves and dive into some real language design!
Remember, setting up these tools might feel like extra work now, but they'll make our lives much easier as the Sodum project grows. Trust me, future you will thank present you for taking the time to set this up properly!
Subscribe to my newsletter
Read articles from Sawez Faisal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sawez Faisal
Sawez Faisal
New to the field and eager to learn how complex systems work smoothly. From building compilers to scalable systems, I’m solving problems as they come—whatever the domain—and sharing all the highs, lows, and lessons along the way!