Most people use the generic apps/ directory with NuttX. That is convenient and well-documented. However, it should always be remembered that NuttX is a stand-alone general purpose OS and has no dependency on that "canned" application directory.

This Wiki page will show you how to create your own, custom application directory from scratch.

Creating the Custom Application Directory

I created a directory to demonstrate the simplest possible custom application directory. It contains only the minimum three files: Makefile, Kconfig, and hello.c

Makefile

The custom application directory must include a Makefile and this makefile must all of the make targets expected by the NuttX build and must generate an archive called libapps.a in the top-level of the custom directory structure. The Makefile has just those minimum required targets:

  APPDIR = ${shell pwd}
  
  -include $(TOPDIR)/Make.defs
  
  # files
  
  CSRCS = hello.c
  COBJS = hello.o
  
  ROOTDEPPATH = --dep-path .
  
  # Build targets
  
  all: libapps.a
  .PHONY: dirlinks context preconfig depend clean clean_context distclean
  .PRECIOUS: libapps$(LIBEXT)
  
  # Compile C Files
  
  $(COBJS): %$(OBJEXT): %.c
  	$(call COMPILE, $<, $@)
  
  # Add object files to the apps archive
  
  libapps.a: $(COBJS)
  	$(call ARCHIVE, libapps.a, $(COBJS))
  
  # Create directory links
  
  dirlinks:
  
  # Setup any special pre-build context
  
  context:
  
  # Setup any special pre-configuration context
  
  preconfig:
  
  # Make the dependency file, Make.deps
  
  depend: Makefile $(CSRCS)
  	$(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
  
  # Clean the results of the last build
  
  clean:
  	$(call CLEAN)
  
  # Remove the build context and directory links
  
  clean_context:
  
  # Restore the directory to its original state
  
  distclean: clean clean_context
  	$(call DELFILE, Make.dep)
  
  # Include dependencies
      
  -include Make.dep
    

Kconfig

A Kconfig file must be included but need not be populated with any meaningful options. This is a place where you can add settings to generate customized builds of your custom application. In the minimum case, Kconfig is only:

    # For a description of the syntax of this configuration file,
    # see the file kconfig-language.txt in the NuttX tools repository.
    #

hello.c

And, of course, you custom application must actually compile some source files in order to generate the required libapps.a archive. One of these source files must include the main() entry point to the application. The function of this main() entry point simply to bring-up the full application. It is called at the completion of OS initialization.

What this application initialization entry point does, how it interacts with the rest of your application, and where the rest of you application code is located is of no concern to the OS. Only this one entry point is needed.

For this example the traditional, over-used, "Hello, World!" application is provded where custom_main() is that application entry point:

    #include <stdio.h>
    
    int custom_main(int argc, char *argv[])
    {
      printf("Hello, World!!\n");
      return 0;
    }

Building with the Custom Application Directory

In order to build with the new custom configuration, you will need the following in your configuration.

    CONFIG_APPS_DIR="../custom-apps"
    CONFIG_USER_ENTRYPOINT="custom_main"

Note that you can only access the ../custom-apps/Kconfig configuration file if CONFIG_APPS_DIR is set to ../custom-apps BEFORE make menucconfig is executed. So if you are starting with an existing configuration, then this is a "chicken-and-egg" problem and an argument for hand-editing the .config file before running make menuconfig.

If you use tools/configure.sh, then a better way to do this is to select the custom-apps directory as a command line option at the time you configure like:

    tools/configure.sh -a ../custom_apps <board>:<config>

Then just build as you normally would. When you execute the program with built with the custom application directory you should see:

    Hello, World!!
  • No labels