CyanogenMod is an enhanced open source firmware distribution for smartphones and tablet computers based on the Android mobileoperating system. It offers features and options not found in the official firmware distributed by vendors of these devices.
Step 1
Before you can begin compiling an android operation system you need to get your computers operating system sorted. You need to be running a Linux operating such as Ubuntu. Ubuntu can be installed natively on it's own or alongside Windows. Ubuntu can also be run within a virtual machine, for details on installing Ubuntu visit their website:
Setting up the Basics
Step 2
The second step of compiling an Android rom is to set up the Build environment, this is the same for all devices and so does not need specific instructions. Please use the official site.
Start at "Setting up a Linux build environment" but do not go into "Configuring USB Access" unless you really want to, it is completely unnecessary.
Setting up the Build Environment
Step 3
Download the repository commands, again these can be found from the official site:
ONLY follow the instructions under "Installing Repo" but don't go into "Initializing a Repo client" we will do that the next step.
Install the repository commands
Step 4
After installing Repo, you need to set up a working directory. Open a terminal and create an empty working directory (mkdir cm-12.1), then move into that directory (cd cm-12.1). Now we sync the repositories for CyanogenMod (repo init -u git:// -b cm-12.1)
Initializing a Repo client
Step 5
The next step is to get the device specific repositories, first we make the correct folder (mkdir .repo/local_manifests) and then make a local manifest (gedit .repo/local_manifests/local_manifest.xml). This command will open the xml file, now open this link ( and paste the contents into that file
Once you have everything sorted run the command (repo sync) to get everything syced up, this will take time. Do not worry, as long as the code is moving things are happening. 
Getting the Galaxy Mega 6.3 specific repositories and the syncing.
Step 6
Once the repositories are all synced to your computer it is time to build the rom, the first command to run is (. build/ and after that (time brunch meliusltexx) this will build the rom.
The rom will take approximately 1 to 3 hours to build depending on your hardware. Once the rom is built you will have a .zip file in cm-12.1/out/target/product/meliusltexx/ this zip file can now be flashed but there are some modifications that could be made. If you wish to flash this rom on the melius3gxx edition of the mega you need to modify the "updater-script", this can be done by opening the .zip file you have made and making your way to /META-INF/com/google/android/ and opening the updater-script. Now you need to remove the first 2 lines. DO NOT REMOVE ifelse(is_mounted("/system"), unmount("/system")); or anything after it! That will break the rom. Once that is done close the file and click update on the zip. You can now flash the rom or rename it if you wish. 
Building the rom
Step 7
You have now successfully compiled a rom! Well done. If you wish to compile the rom again you will need to clean your out directories. This can be done with the clean command (make clean), after this the sync command (repo sync) and after that (. build/ and (time brunch meliusltexx) and congrats you just compiled for a second time!
Now perhaps try compiling another rom.
Compiling the rom again
Courtesy of wopema71