Search code examples
makefilegnu

Can someone explain what is being assigned into use_database?


I understand that the := operator is a simple assignment that only gets assigned one and that = is a recursive assignment. But when they are combined I am confused and cannot figure out what is going on in this code block:

use_database := MAIN_DATABASE=sqlite
$(use_database) python3 manage.py makemigrations amlcenter

My first thought was that it assigned sqlite to both use_database and MAIN_DATABASE but I don't think that's it. Then I thought it assigned "MAIN_DATABASE=sqlite" to use_database but that would make the second line:

MAIN_DATABASE=sqlite python3 manage.py makemigrations amlcenter

which I don't feel makes sense. Any help would be appreciated. This is in a Makefile.

use_database := MAIN_DATABASE=sqlite
use_runserver_str := python3 manage.py runserver 0.0.0.0:8050
use_runscript_str := python3 manage.py runscript

db_migrate:  ## Db migrate
    $(use_database) python3 manage.py makemigrations amlcenter
    $(use_database) TEST_MODE=True python3 manage.py migrate

clean: ## Clean Directory
    rm -f db.sqlite3
    rm -rf static/
    rm -rf media/
    rm -f aml.log

dev: clean db_migrate  ## Set up development server with sample data
    if [[ $(use_database) = *"psql"* ]] ; then $(use_database) python3 manage.py flush --noinput; echo 'Flushed psql'  ; fi
    $(use_database) $(use_runscript_str) sample_data_generator

Solution

  • The shell syntax var=value cmd args temporarily sets the variable var to the value value for the duration of the execution of cmd args.

    Apparently this would be used inside a Makefile recipe, and apparently the Python script which runs will examine its environment to pick up this variable, probably something like

    import os
    
    # ...
    if os.environ('MAIN_DATABASE') == 'sqlite':
        do_sqlitey_things()
    # probably else if it's 'mysql', do mysqly things or Postgressy things for 'postgres', etc