JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
General

Importing Other File Types to CodeSonar

There are two main use cases for direct file import.



Overview

There are two main use cases for direct file import.

Note

In this section (and throughout this manual), $CSONAR indicates the CodeSonar installation directory.

Importing Files

The following diagram provides an overview of a CodeSonar build process that brings files into the CodeSonar project.

diagram: building and analyzing a CodeSonar project with an add_source_files.py component

As described in Command Line Build/Analysis: Command, the general form of the usual CodeSonar build/analysis command is

codesonar analyze /path/to/pfiles-name [[protocol://]host:port] [other_options] [command]

To import one or more files, command must incorporate one or more invocations of codesonar add_source_files.py.

codesonar add_source_files.py

To import files or directory contents to a project, CodeSonar observes the execution of a command that incorporates one or more invocations of

codesonar add_source_files.py [file_or_dir ...] [-include file_pat] [-exclude file_pat] \
[-language lang] [-max-bytes num]

where

[file_or_dir ...] is a space-separated list of source file or directory paths.
  • For each file path, that file is imported.
  • For each directory path, all files in the directory tree rooted at that directory are imported.

Note:

  • These files are imported regardless of any -exclude options: an -exclude option can only modify the effects of preceding -include options. To avoid confusion, we recommend specifying the files to import through direct arguments or through a series of -include/-exclude options, but not both.
  • The file_or_dir arguments are not glob patterns (as used for -include and -exclude). However, your shell may expand patterns into file and directory names.

If no file or directory paths are specified and the set of additional files specified with -include and -exclude is empty, the codesonar add_source_files.py subcommand has no effect.

The importer will ignore files with certain extensions, including those that indicate images (.jpg, .gif,...), archives (.tar, .gz,...) and library/object files (.o, .lib, .dll,...).
[-include file_pat]
[-exclude file_pat]
Specify an additional set of source files to be imported.

file_pat is a a recursive glob pattern representing a source file path. It matches all files whose file path matches file_pat.

When a single codesonar add_source_files.py invocation contains a combination of -include and -exclude options, the set of additional files is determined by applying the corresponding inclusions and exclusions in the order in which they appear on the command line.

Note:

  • Your shell may be configured to expand patterns into file and directory names. If so, make sure you quote the file_pat appropriately to indicate that the shell should pass it through to codesonar add_source_files.py. In most cases, this means using single quotes ('); for Windows cmd, use double quotes (").
  • The -exclude option can only modify the effects of preceding -include options: it does not affect any files or directories specified as direct arguments to codesonar add_source_files.py. To avoid confusion, we recommend specifying the files to import through direct arguments or through a series of -include/-exclude options, but not both.
[-language lang] Specifies that the imported files should be recorded as having language lang.

If -language is not specified, CodeSonar will attempt to determine the language for each file based on its file extension. If it cannot determine a language, the file language will be recorded as "text".

The available lang values are:
actionscript, ada, awk, c, cfml, clojure, cobol, cpp, csharp, d, dart, eiffel, erlang, forth, fortran, fsharp, go, haskell, java, javascript, julia, kotlin, lisp, lua, objective_c, pascal, perl, php, prolog, python, r, ruby, rust, sas, scala, scheme, smalltalk, sql, swift, text, typescript, vbscript, verilog, vhdl, visualbasic

  • The file language will be displayed in the UI and can be accessed programmatically through the CodeSonar Plug-in API.
  • Specifying a language with this option does not cause the importer to apply language-specific parsing and other handling to the imported files.
    If you have source files in tier 1 (C, C++) or tier 2 (Java, C#) languages, use the appropriate language-specific mechanism to include them in the CodeSonar project.
[-max-bytes num] Specifies a maximum size of num bytes for imported files: files larger than this maximum size will be ignored by the importer.
If -max-bytes is not specified, the importer will ignore files larger than 500KB.
  • You will need to specify this option with a suitably high value if you wish to import source files that are larger than the default limit.
  • You may wish to specify this option with a lower value in cases where you know that all your source files are relatively small and wish to protect against accidentally importing larger, irrelevant files.

Command Examples

Typical Use Case

Suppose we want to import a ReadMe file into the CodeSonar project.

  1. Start with your normal software build.
    In most cases, this will be managed through some build system (such as make) so that you can perform the build by executing a single command instead of performing all compilations and other operations manually.
  2. Define an extended variant of your normal software build that performs all the steps of the regular build, then invokes codesonar add_source_files.py to add the ReadMe file to the CodeSonar project.

    This is your CodeSonar-facing build.

  3. Perform the CodeSonar build/analysis, observing the entire execution of your CodeSonar-facing build. For example:
    Build Tool Example regular build command Example CodeSonar-facing build
    make make all make csonar_scan
    Where the Makefile has been extended to include a csonar_scan target that depends on all and has a recipe that includes invocations of codesonar add_source_files.py.

    Then the CodeSonar build/analysis command will be:

    codesonar analyze myProject make csonar_scan
    Windows batch file cmd /c myProj.bat cmd /c myProj.bat CSonarScan
    Where myProj.bat has been extended to invoke codesonar add_source_files.py after the normal build steps have been performed, but only if argument CSonarScan is passed.

    Then the CodeSonar build/analysis command will be:

    codesonar analyze myProject cmd /c myProj.bat CSonarScan
    (and so forth)
    For more examples demonstrating how to construct a CodeSonar build/analysis command for various build tools, see Command Line Build/Analysis: Language-Specific Examples and the Basic Tutorial.

Example

Suppose part of your software project involves a rare programming language: so rare it does not have an analysis ecosystem.

If you use make to manage your regular software build, your Makefile might look something like the following.
(If you use another tool to manage your regular build then the mechanism for expressing the steps and dependencies will be different, but analogous.)

.PHONY: all clean

# RareCompSrc.rare: a rarelang source file
# rarecompile: the compiler for rarelang
RareCompExe: RareCompSrc.rare
       rarecompile RareCompSrc.rare -o RareCompExe

all: RareCompExe othercomponent1 [... other project components]

clean:
        rm -f RareCompExe

[... recipes for remaining project components]

If you built a CodeSonar project based on observing the execution of this Makefile in its original state, the CodeSonar project would not contain any information about the rarelang component.

To import RareCompSrc.rare into your CodeSonar project, do the following.

  1. Build and analyze a CodeSonar project based on observing the execution of the original Makefile. Assuming your hub is at the default location, the CodeSonar build/analysis command will be something like:
    make clean
    codesonar analyze ProjectX make all
    Once the CodeSonar build/analysis is complete, open its hub GUI Analysis page.
  2. Edit the Makefile to add a new csonar_scan target. This target should: The updated example Makefile is shown below, with the new contents highlighted like this.
    .PHONY: all clean csonar_scan
    
    RareCompExe: RareCompSrc.rare
           rarecompile RareCompSrc.rare -o RareCompExe
    
    all: RareCompExe othercomponent1 [... other project components]
    
    clean:
            rm -f RareCompExe
    
    csonar_scan: RareCompSrc.rare
            codesonar add_source_files.py RareCompSrc.rare
    
    [... recipes for remaining project components]
    
    If you are not using make, add these invocations in the appropriate analogous location in your build infrastructure.
  3. Build and analyze a CodeSonar project, using a codesonar analyze command based on building the new csonar_scan target.
    make clean
    codesonar analyze ProjectX make csonar_scan
  4. Once the CodeSonar build/analysis is complete, open its hub GUI Analysis page.
 

To report problems with this documentation, please visit https://support.codesecure.com/.